Reputation: 157
Given this dataframe
df = pd.DataFrame(\
{'name': {0: 'Peter', 1: 'Anna', 2: 'Anna', 3: 'Peter', 4: 'Simon'},
'Status': {0: 'Finished',
1: 'Registered',
2: 'Cancelled',
3: 'Finished',
4: 'Registered'},
'Modified': {0: '2019-03-11',
1: '2019-03-19',
2: '2019-05-22',
3: '2019-10-31',
4: '2019-04-05'}})
How can I compare and filter based on the Status? I want the Modified column to keep the date where the Status is either "Finished" or "Cancelled", and fill in blank where the condition is not met.
Wanted output:
name Status Modified
0 Peter Finished 2019-03-11
1 Anna Registered
2 Anna Cancelled 2019-05-22
3 Peter Finished 2019-10-31
4 Simon Registered
Upvotes: 0
Views: 7750
Reputation: 323226
Check with where
+ isin
df.Modified.where(df.Status.isin(['Finished','Cancelled']),'',inplace=True)
df
Out[68]:
name Status Modified
0 Peter Finished 2019-03-11
1 Anna Registered
2 Anna Cancelled 2019-05-22
3 Peter Finished 2019-10-31
4 Simon Registered
Upvotes: 4