Python 获取不同值计数大于指定值的列
设想以下Python数据帧:Python 获取不同值计数大于指定值的列,python,pandas,group-by,pandas-groupby,Python,Pandas,Group By,Pandas Groupby,设想以下Python数据帧: df = pd.DataFrame({'id' : ['foo', 'bar', 'foo'], \ 'A' : ['property1', 'property1', 'property2'], \ 'B' : ['test', 'test', 'test'] }) from tabulate import tabulate print(tabulate(df, headers='keys'
df = pd.DataFrame({'id' : ['foo', 'bar', 'foo'], \
'A' : ['property1', 'property1', 'property2'], \
'B' : ['test', 'test', 'test'] })
from tabulate import tabulate
print(tabulate(df, headers='keys', tablefmt='psql'))
+----+-----------+------+------+
| | A | B | id |
|----+-----------+------+------|
| 0 | property1 | test | foo |
| 1 | property1 | test | bar |
| 2 | property2 | test | foo |
+----+-----------+------+------+
在这里您可以看到,对于id“foo”列,B只有一个唯一的(不同的)值,即test。但对于列A,它有两个不同的值property1和property2。对于id“bar”,两列只有一个不同的值
我要寻找的是一段代码,它给出了计数大于1的列的名称,如果按id分组。因此,结果应该是列A的名称,因为它包含非不同的值
df.groupby(['id'])
我只知道如何获取计数(发生)大于1的ID。但这不是我最终想要的
df['id'].value_counts().reset_index(name="count").query("count > 1")["id"]
感谢您的提示。使用:
#filter column of interest
a = (df.groupby(['id'])['A','B'].nunique() > 1).any()
print (a)
A True
B False
dtype: bool
#if need test all columns without id
a = (df.set_index('id').groupby('id').nunique() > 1).any()
print (a)
A True
B False
dtype: bool
最后一个过滤器:
b = a.index[a]
print (b)
Index(['A'], dtype='object')
也许你在寻找:
g = df.groupby('id')['A', 'B'].nunique()
g
A B
id
bar 1 1
foo 2 1
要获取相关列,只需索引到df.columns
:
df.columns[(g > 1).any()]
Index(['A'], dtype='object')
更新:
In [98]: df.columns.drop('id')[(df.groupby('id')[df.columns.drop('id')].nunique() > 1).any()]
Out[98]: Index(['A'], dtype='object')
说明:
In [32]: df.groupby(['id'])['A','B'].apply(lambda x: x.nunique().gt(1))
Out[32]:
A B
id
bar False False
foo True False
In [33]: df.groupby(['id'])['A','B'].apply(lambda x: x.nunique().gt(1)).any()
Out[33]:
A True
B False
dtype: bool
这是另一种方法
pd.crosstab(df.id,[df.A,df.B],margins =True)
Out[206]:
A property1 property2 All
B test test
id
bar 1 0 1
foo 1 1 2
All 2 1 3
或者类似的
[x if df.groupby(['id',x]).ngroup().max()>1 else np.nan for x in df.columns]
Out[233]: ['A', nan, nan]
你能发布你想要的数据集吗?不,因为它是保密的。因此,我添加了一些演示数据。我指的是基于示例输入数据集的所需数据集…这就是我的想法:-),然后如何过滤该数据帧以获得列名“A”,这是唯一一个值大于1的列。@Matthias
df.columns.difference(['id'])[(df.groupby('id')['A',B'].nunique()>1).any()]
-->给出一个@Bharath df.columns包含id,而索引器不包含id。我同意,但我认为由于我们使用groupby,id
将始终具有假值,因为没有重复项。第二个代码段也是我所想的。但我不是在寻找值,而是寻找保存这些值的列名。这是因为原始数据集大约有32列。很好,效果很好。我使用代码检查所有列,但没有设置索引。很抱歉,我没有得到IINDELING部分。但是它可以工作。@SourabhMaity-它通过布尔值过滤a
系列的索引-因此在a.index[a]
之后,只获取真正值的索引。尽管您仍然需要从那里获取列名。找不到方法.gt,但使用了>1。无论如何,有没有一种方法可以不使用列名a和B以及所有列的列表来获取此代码,因为原始数据有很多列需要测试。
[x if df.groupby(['id',x]).ngroup().max()>1 else np.nan for x in df.columns]
Out[233]: ['A', nan, nan]