[英]Check Unique Column Values Based on Combination of other Column Value
I want to be able to check if one column always has a unique set in the other columns.我希望能够检查一列是否在其他列中始终具有唯一的集合。 For example, in this dataframe, I would like to test if Orange always has a value of yes, yes in stale and stock and apple always has a value of yes, no.例如,在此数据框中,我想测试 Orange 是否始终具有值 yes、yes 在 stale 和 stock 中,而 apple 的值是否始终为 yes、no。 Please give me instructions on how to do this for a larger dataframe in order to check if one volumn is mapped form the unique combination of another two columns.请给我有关如何对更大的数据框执行此操作的说明,以检查一个卷是否从另外两列的唯一组合映射。
type stale stock
orange yes yes
apple yes no
orange yes yes
you can use drop_duplicates
to remove all duplicate rows (based on relevant columns) from the df, then use groupby
on type and check that size()
is equal to 1
.您可以使用drop_duplicates
从 df 中删除所有重复行(基于相关列),然后在 type 上使用groupby
并检查size()
是否等于1
。
try this:尝试这个:
print(df.drop_duplicates(['type', 'stale', 'stock']).groupby('type').size().eq(1))
Output:输出:
type
apple True
orange True
dtype: bool
and here's a bigger example with some values that also don't have a unique set:这是一个更大的例子,其中一些值也没有唯一的集合:
import pandas as pd
from io import StringIO
s = """
type stale stock
orange yes yes
apple yes no
orange yes yes
orange yes yes
banana yes yes
banana yes no
peach no no
peach yes no
"""
df = pd.read_csv(StringIO(s), sep="\s+")
print(df.drop_duplicates(['type', 'stale', 'stock']).groupby('type').size().eq(1))
Output:输出:
type
apple True
banana False
orange True
peach False
dtype: bool
我不确定您是想找出数据框中存在的所有值,还是仅针对单个给定值,例如,如果您想为“橙色”执行此操作,则可以使用以下命令:
df.loc[df["type"] == "orange"].nunique().stale == 1 & df.loc[df["typee"] == "orange"].nunique().stock == 1
Can groupby size twice.可以按大小分组两次。 The first collapses to unique combinations, the second checks if there is only one per type.第一个折叠为唯一的组合,第二个检查每种类型是否只有一个。
df.groupby([*df]).size().groupby('type').size().eq(1)
#type
#apple True
#orange True
#dtype: bool
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.