简体   繁体   English

如何检查 pandas dataframe 列中的所有值是否相等?

[英]How do I check if all values in a column of a pandas dataframe are equal?

I have a dataframe like this我有一个像这样的 dataframe

  name data result 
0  x    100
1  x    100
2  x    100
3  x    100
4  x    100
5  y    100
6  y    90
7  y    90
8  y    100
9  y    85

I want to check whether each group in the name column have the same value in the data column.我想检查name列中的每个组在data列中是否具有相同的值。

So for each x group, if the corresponding data value are all equal, write full in the result column.所以对于每个x组,如果对应的data值都相等,则在result列中full If the values for a group not are all equal, write nearly in the result column.如果一个组的值不是全部相等,请在result列中写入nearly

I have tried grouping the dataframe:我尝试将 dataframe 分组:

dfx = df.groupby('name')
dfx = dfa.get_group('x')

but it doesn't really help in checking if each value is the same, write in the result column.但它并不能真正帮助检查每个值是否相同,请写入result列。

I have tried creating a function that will check for unique values我尝试创建一个 function 来检查唯一值

def check_identicals(row):
    if(df.sent.nunique() == 1):
        print('Full')

The idea here is to then apply that function to each row and write the output in the result column.这里的想法是然后将 function 应用到每一行,并在result列中写入 output。

Ideal output:理想output:

   name data result 
0  x    100   full
1  x    100   full
2  x    100   full
3  x    100   full
4  x    100   full
5  y    100   nearly
6  y    90    nearly
7  y    90    nearly
8  y    100   nearly
9  y    85    nearly

Use numpy.where with GroupBy.transform and DataFrameGroupBy.nunique for compare all values in new Series with same size like original DataFrame :使用numpy.whereGroupBy.transformDataFrameGroupBy.nunique比较新Series中与原始DataFrame相同大小的所有值:

df['result'] = np.where(df.groupby('name')['data'].transform('nunique') == 1,'full','nearly')
print (df)
  name  data  result
0    x   100    full
1    x   100    full
2    x   100    full
3    x   100    full
4    x   100    full
5    y   100  nearly
6    y    90  nearly
7    y    90  nearly
8    y   100  nearly
9    y    85  nearly

EDIT:编辑:

For test if all missing values per groups use numpy.select with another condition with compare mising values with transform and GroupBy.all :为了测试每组的所有缺失值是否使用numpy.select和另一个条件,将缺失值与transformGroupBy.all进行比较:

m1 = df.groupby('name')['data'].transform('nunique') == 1
m2 = df['data'].isna().groupby(df['name']).transform('all')

df['result'] = np.select([m1, m2], ['full', 'all_missing'],'nearly')
print (df)
  name   data       result
0    x  100.0         full
1    x  100.0         full
2    x  100.0         full
3    x  100.0         full
4    x  100.0         full
5    y  100.0       nearly
6    y   90.0       nearly
7    y   90.0       nearly
8    z    NaN  all_missing
9    z    NaN  all_missing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查列中的所有值是否相等 PySpark Dataframe - Check if all values of a column are equal in PySpark Dataframe 如何根据 Python 中的列值将 pandas dataframe 单元格设置为等于? - How do I set pandas dataframe cells equal to based on column values in Python? 如何将列表值与不完全相等的数据框列进行比较? - How do I compare list values to a dataframe column that are not exactly equal? 使用 Python,如何在保留/忽略所有“nan”值的同时删除 PANDAS dataframe 列中的重复项? - Using Python, how do I remove duplicates in a PANDAS dataframe column while keeping/ignoring all 'nan' values? 如何将所有以前的值放入熊猫数据框中列中的列表中? - How do I bring all previous values into a list in column in pandas dataframe? 如何从pandas dataframe列中的元组中的一个位置获取所有值? - How do I get all values from one position in a tuple in a pandas dataframe column? 如何在Pandas DataFrame中检查列值的类型 - How to check a type of column values in pandas DataFrame 编写检查所有 Pandas DataFrame 列值是否满足特定值? - Writing check that all pandas DataFrame column values meet a certain values? 如何访问熊猫数据帧的第一列以外的所有内容? - How do I access all but the first column of a pandas DataFrame? 如何反转 pandas dataframe 列中的所有列表? - How do I reverse all lists in a pandas dataframe column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM