1

I have the following dataframe:

df
Date Application Weekday
02.01 A           3
02.01 B           3
03.01 A           4
03.01 B           4
04.01 A           5
04.01 B           5
05.01 A           6
05.01 B           6

And I want to filter out the weekdays: 5,6 and want to have a new index based on the dates, that means i would like to get:

df
Date Application Weekday Index
02.01 A           3        1
02.01 B           3        1 
03.01 A           4        2
03.01 B           4        2

How can i get that?

1 Answer 1

1

Use boolean indexing and create new column with DataFrame.assign and factorize, lambda function is necessary for processing filtered data:

df = df[df['Weekday'].lt(5)].assign(Index = lambda x: pd.factorize(x['Date'])[0] + 1)
print (df)
   Date Application  Weekday  Index
0  2.01           A        3      1
1  2.01           B        3      1
2  3.01           A        4      2
3  3.01           B        4      2
Sign up to request clarification or add additional context in comments.

1 Comment

I get the error: ValueError: Length of values does not match length of index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.