计算组内的值

时间:2018-01-21 11:18:14

标签: python pandas

我如何计算每年所有人物='来宾'和所有人物='陌生人',如果客人人数较多,则陌生人数分别定义一年'陌生年'或'客人年'

g=titles.head()


name       type     character   n
Homo       actor    Guests      22.0
Ramon      actor    Owner       NaN
'El Guisa' actor    Bailaor     25.0
'El Viti'  actor    Cantaor     20.0
Murray     actor    Himself     NaN

1 个答案:

答案 0 :(得分:0)

IIUC:

假设你有以下DF:

In [58]: df
Out[58]:
   year character
0  2010    Guests
1  2010  Stranger
2  2010  Stranger
3  2011    Guests
4  2011    Guests
5  2011  Stranger

选项1:

In [59]: df.groupby('year')['character'].apply(lambda x: x.value_counts().idxmax())
Out[59]:
year
2010    Stranger
2011      Guests
Name: character, dtype: object

选项2:

In [61]: df.groupby('year')['character'].apply(lambda x: x.value_counts().nlargest(1)).reset_index()
Out[61]:
   year   level_1  character
0  2010  Stranger          2
1  2011    Guests          2