pandas "DataFrame"对象没有属性“唯一" [英] Pandas 'DataFrame' object has no attribute 'unique'

查看:178
本文介绍了 pandas "DataFrame"对象没有属性“唯一"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在熊猫工作中做数据透视表和分组时(计算不同的观察值) aggfunc={"person":{lambda x: len(x.unique())}}给我以下错误: 'DataFrame' object has no attribute 'unique' 有任何解决方法的想法吗?

I'm working in pandas doing pivot tables and when doing the groupby (to count distinct observations) aggfunc={"person":{lambda x: len(x.unique())}} gives me the following error: 'DataFrame' object has no attribute 'unique' any ideas how to fix it?

推荐答案

DataFrames没有该方法.数据框架中的列可以做到:

DataFrames do not have that method; columns in DataFrames do:

df['A'].unique()

或者,要获取带有观察值数量的名称(使用由closedloop给出的DataFrame):

Or, to get the names with the number of observations (using the DataFrame given by closedloop):

>>> df.groupby('person').person.count()
Out[80]: 
person
0         2
1         3
Name: person, dtype: int64

这篇关于 pandas "DataFrame"对象没有属性“唯一"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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