Reputation: 690
Suppose that I have this minimal DataFrame to represent my needs:
Tag Year Month Snowed
0 0 2001 Jan True
1 0 2001 Feb False
2 0 2001 Mar True
3 0 2002 Jan True
4 0 2002 Feb True
5 0 2002 Mar False
How could I fill the Tag column such that, for each year, it is tagged as BAD if snowed in both Jan&Feb or GOOD otherwise?
Upvotes: 0
Views: 62
Reputation: 323226
I would use transform
, all
, and ffill
:
df['Tage']=df.loc[df.Month.isin(['Jan','Feb'])].groupby('Year')['Snowed'].transform('all').map({False:'Good',True:'Bad'})
df.ffill(inplace=True)
df
Out[262]:
Tag Year Month Snowed Tage
0 0 2001 Jan True Good
1 0 2001 Feb False Good
2 0 2001 Mar True Good
3 0 2002 Jan True Bad
4 0 2002 Feb True Bad
5 0 2002 Mar False Bad
s=df.loc[df.Month.isin(['Jan','Feb'])].groupby('Year')['Snowed'].agg('all').map({False:'Good',True:'Bad'})
Then map
back
df['Tage']=df.Year.map(s)
Upvotes: 1
Reputation: 484
Along with the above answer, this also works. This code might be less confusing though.
Since we don't care about the data in March as per your question:
df = df[(df.Month != 'Mar')]
def True_Or_Not(df):
Result = df['Snowed'].all(axis=None) == True
df['Result'] = Result
return df
Groupby Year and aggregate month column to determine whether all values are true
df = df.groupby(['Year']).apply(lambda df: True_Or_Not(df)).reset_index().drop(columns = ['index'])
df['Tag'] = ['Good' if i== False else 'Bad' for i in df.Result.tolist()]
OUTPUT:
Tag Year Month Snowed Result
0 Good 2001 Jan True False
1 Good 2001 Feb False False
2 Bad 2002 Jan True True
3 Bad 2002 Feb True True
Upvotes: 0