python pandas,DF.groupby().agg(),agg()中的列引用 [英] python pandas, DF.groupby().agg(), column reference in agg()

查看:31
本文介绍了python pandas,DF.groupby().agg(),agg()中的列引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个具体的问题上,假设我有一个 DataFrame DF

On a concrete problem, say I have a DataFrame DF

     word  tag count
0    a     S    30
1    the   S    20
2    a     T    60
3    an    T    5
4    the   T    10 

我想找到对于每个单词",具有最多计数"的标签".所以回报将类似于

I want to find, for every "word", the "tag" that has the most "count". So the return would be something like

     word  tag count
1    the   S    20
2    a     T    60
3    an    T    5

我不关心计数列,也不关心订单/索引是原始的还是混乱的.返回字典 {'the' : 'S', ...} 就好了.

I don't care about the count column or if the order/Index is original or messed up. Returning a dictionary {'the' : 'S', ...} is just fine.

我希望我能做到

DF.groupby(['word']).agg(lambda x: x['tag'][ x['count'].argmax() ] )

但它不起作用.我无法访问列信息.

but it doesn't work. I can't access column information.

更抽象地说,agg(function) 中的 function 将什么视为其参数?

More abstractly, what does the function in agg(function) see as its argument?

顺便说一句,.agg() 和 .aggregate() 是一样的吗?

btw, is .agg() the same as .aggregate() ?

非常感谢.

推荐答案

aggaggregate 相同.可调用的是传递 DataFrame 的列(Series 对象),一次一个.

agg is the same as aggregate. It's callable is passed the columns (Series objects) of the DataFrame, one at a time.

您可以使用 idxmax 收集最大行的索引标签计数:

You could use idxmax to collect the index labels of the rows with the maximum count:

idx = df.groupby('word')['count'].idxmax()
print(idx)

收益

word
a       2
an      3
the     1
Name: count

然后使用 loc 选择 wordtag 列中的那些行:

and then use loc to select those rows in the word and tag columns:

print(df.loc[idx, ['word', 'tag']])

收益

  word tag
2    a   T
3   an   T
1  the   S

注意 idxmax 返回索引 labels.df.loc 可以用来选择行按标签.但是如果索引不是唯一的——也就是说,如果有带有重复索引标签的行——那么 df.loc 将选择所有行,其中的标签列在 <代码>idx.所以要小心 df.index.is_uniqueTrue 如果你使用 idxmaxdf.loc

Note that idxmax returns index labels. df.loc can be used to select rows by label. But if the index is not unique -- that is, if there are rows with duplicate index labels -- then df.loc will select all rows with the labels listed in idx. So be careful that df.index.is_unique is True if you use idxmax with df.loc

或者,您可以使用 apply.apply 的可调用对象被传递一个子 DataFrame,它使您可以访问所有列:

Alternative, you could use apply. apply's callable is passed a sub-DataFrame which gives you access to all the columns:

import pandas as pd
df = pd.DataFrame({'word':'a the a an the'.split(),
                   'tag': list('SSTTT'),
                   'count': [30, 20, 60, 5, 10]})

print(df.groupby('word').apply(lambda subf: subf['tag'][subf['count'].idxmax()]))

收益

word
a       T
an      T
the     S

<小时>

使用idxmaxloc 通常比apply 更快,尤其是对于大型数据帧.使用 IPython 的 %timeit:


Using idxmax and loc is typically faster than apply, especially for large DataFrames. Using IPython's %timeit:

N = 10000
df = pd.DataFrame({'word':'a the a an the'.split()*N,
                   'tag': list('SSTTT')*N,
                   'count': [30, 20, 60, 5, 10]*N})
def using_apply(df):
    return (df.groupby('word').apply(lambda subf: subf['tag'][subf['count'].idxmax()]))

def using_idxmax_loc(df):
    idx = df.groupby('word')['count'].idxmax()
    return df.loc[idx, ['word', 'tag']]

In [22]: %timeit using_apply(df)
100 loops, best of 3: 7.68 ms per loop

In [23]: %timeit using_idxmax_loc(df)
100 loops, best of 3: 5.43 ms per loop

<小时>

如果你想要一个字典将单词映射到标签,那么你可以使用 set_indexto_dict 像这样:

In [36]: df2 = df.loc[idx, ['word', 'tag']].set_index('word')

In [37]: df2
Out[37]: 
     tag
word    
a      T
an     T
the    S

In [38]: df2.to_dict()['tag']
Out[38]: {'a': 'T', 'an': 'T', 'the': 'S'}

这篇关于python pandas,DF.groupby().agg(),agg()中的列引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆