how to use columns values to groupby

ⅰ亾dé卋堺 提交于 2019-12-11 10:35:03

问题


I need to get the top1 and top2 rating watched by 'ma' and 'young'. here I only need to specifically define my value but not column usinga group by.

data:

gender  age rating
ma  young   PG
fe  young   PG
ma  adult   PG
fe  adult   PG
ma  young   PG
fe  young   PG
ma  adult   R
fe  adult   R
ma  young   R
fe  young   R

code :

top1 = df.groupby(['ma','young']])['rating'].apply(lambda x: x.value_counts().index[0])
top2 = df.groupby(['ma','young']])['rating'].apply(lambda x: x.value_counts().index[1])

Please let me know how do i do it.


回答1:


First filter and then get tops, but general is possible second top should not exist:

df1 = df.query("gender== 'ma' & age == 'young'")
#alternative is boolean indexing
#df1 = df[(df['gender'] == 'ma') & (df['age'] == 'young')]
tops = df1.groupby(['gender','age'])['rating'].value_counts()
print (tops)
gender  age    rating
ma      young  PG        2
               R         1

print (df.iloc[[0]])
  gender    age rating
0     ma  young     PG


print (df.iloc[[1]])
  gender    age rating
1     fe  young     PG


来源:https://stackoverflow.com/questions/48741867/how-to-use-columns-values-to-groupby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!