简体   繁体   English

根据其他列值的组合检查唯一列值

[英]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.

相关问题 根据其他列的唯一组合更改数据框列值 - Change the dataframe column values based on unique combination of other columns 根据其他列计算唯一值的计数 - Calculate counts of unique values based on other column 基于其他列条件的唯一值计数 - Count of unique values based on conditions of other column 根据其他列的唯一值重命名列值 - Rename column values based on unique values of other column 在列中为其他列中的每个唯一值查找唯一值的计数 - Finding counts of unique values in column for each unique value in other column 基于其他列 python 的特定值的每列的唯一值计数 - counts of unique values for each column based on specific value of other column python 根据匹配的列值与其他数据框的组合删除行熊猫 - Drop rows pandas based on combination of matched column values with other dataframe 使用基于(非唯一)列值的其他行中的值替换 DataFrame 行中的 NaN 值 - Replacing NaN values in a DataFrame row with values from other rows based on a (non-unique) column value 为列值的唯一组合创建 ID 号 - create id number for unique combination of column values 将列的值放入新列并检查其他列中的重复项,如果其他列重复,则在值列中打印 1 - Make a values of column into new column and check for duplicates in other column ,print 1 in the value column if other column duplicated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM