python - How to count the number of times a value is in a column based on particular row values? -
i have data frame:
outlook temperature playtennis value 0 sunny 60 yes 1 1 sunny 70 yes 1 2 sunny 40 no 1 3 overcast 40 no 1 4 overcast 60 yes 1 5 overcast 50 yes 1 6 overcast 70 yes 1 7 overcast 80 yes 1 8 rain 65 no 1 9 rain 70 yes 1
and want this
outlook yes no sunny 2 1 overcast 4 1 rain 1 1
not sure commands use count yesses , nos based on sunny/overcast/rain
how's this?
df.groupby('outlook').apply(lambda g: g['playtennis'].value_counts())
or, exact spec:
df.groupby('outlook').apply(lambda g: g['playtennis'].value_counts()).unstack(1)
or shorter:
df.groupby('outlook')['playtennis'].value_counts().unstack(1)
Comments
Post a Comment