如何使用列值进行分组 [英] how to use columns values to groupby
本文介绍了如何使用列值进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获得"ma"和"young"观看的top1和top2评分.在这里,我只需要专门定义我的值,而无需使用group by来定义列.
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.
数据:
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
代码:
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.
推荐答案
首先过滤,然后获得顶部,但一般来说,第二顶部可能不存在:
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
这篇关于如何使用列值进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文