简体   繁体   English

如果值相同,如何检查 3 列是否相同并添加一个具有该值的新列?

[英]How to check if 3 columns are same and add a new column with the value if the values are same?

I have a dataframe that look like this..我有一个看起来像这样的数据框..

index   Column A    Column B    Column C
0        alice       alice       alice
1        nick        nick        john
2        juli        nick        alice

I want to check in Column A, Column B and Column C are equal or not.我想检查Column A, Column B and Column C是否相等。 If equal, I want to add the the value as a new Column D .如果相等,我想将该值添加为新的Column D If not, add None to Column D如果不是,则将None添加到Column D

I did this so far..到目前为止我这样做了..

def func(row):
    if ((row['Column A']) == (row['Column B']) == (row['Column C'])):
        df['Column D'] = df['Column A']
    else:
        df['Column D'] = None

When I applied the function using.. df.apply (lambda row: func(row),axis =1) , I am not getting the desired output.当我使用 .. df.apply (lambda row: func(row),axis =1)应用该函数时,我没有得到所需的输出。

I got something like this..我有这样的东西..

index   Column A    Column B    Column C    Column D
0      alice        alice       alice        None
1      nick         nick        john         None
2      juli         nick        alice        None

whereas, I want the output to be like..而,我希望输出像..

index   Column A    Column B    Column C    Column D
0      alice        alice       alice        alice
1      nick         nick        john         None
2      juli         nick        alice        None

Any help on this?这有什么帮助吗?

use numpy where使用numpy where

here you are taking a subset of the dataframe to compare and store to an array arr then comparing the first column of the array against the rest of the columns.在这里,您将数据帧的一个子集进行比较并存储到数组arr然后将数组的第一列与其余列进行比较。

import numpy as np
arr = df[['A','B','C']].values
df['D'] = np.where((arr == arr[:, [0]]).all(axis=1),df['A'],None)

or或者

def func(row):
    if ((row['A']) == (row['B']) == (row['C'])):
        return row['A']
    else:
        return None

df['D'] = df.apply(lambda row: func(row),axis =1)

In your if clause you wrote:在您的 if 子句中,您写道:

(row['Column A']) == (row['Column B']) == (row['Column C'])

I'm not sure if it is the right way to do it.我不确定这是否是正确的方法。 Have you tried this code below as your if clause?你有没有试过下面的这段代码作为你的 if 子句?

((row['Column A']) == (row['Column B'])) and ((row['Column B']) == (row['Column C']))

I tried df['Column D'] = np.where((((df['Column A'])==(df['Column B']))& (df['Column B'] == df['Column C'])),df['Column A'],None)我试过df['Column D'] = np.where((((df['Column A'])==(df['Column B']))& (df['Column B'] == df['Column C'])),df['Column A'],None)

and this worked!这有效! Thanks all for giving the idea.感谢大家提供的想法。

暂无
暂无

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

相关问题 如何将一列的值除以前一行的列(而不是同一列),并将结果作为新维度添加到numpy数组中? - How to divide one columns value by a previous rows column(not the same column) and add the result as a new dimension to a numpy array? 如何将具有(某些)相同值的两列的值移动到新列 - How to shift the values of two columns with (some) same values to a new column pandas:如何检查列值是否在同一行的其他列中 - pandas: how to check if a column value is in other columns in the same row 将 dataframe 中的相同列值分组,并将相同值的总和添加为新列 - Group the same column value in the dataframe and add the sum of the same values as a new column 使用相同的默认值向 DataFrame 添加新列 - Add new column to DataFrame with same default value 如果比较相同的值,如何比较列的值并添加计数器 - how to compare values of a column and add a counter if the same value is found 熊猫如何检查特定列的值是相同的 - Pandas how to check values of specific columns is same 如何添加来自同一列的两个值? - How to add two Values from the same columns? 比较 2 个 pandas 数据框列并根据值是否相同创建新列 - Comparing 2 pandas dataframe columns and creating new column based on if the values are same or not 根据多列中的值和相同条件在熊猫中创建新列 - Create a new column in pandas based on values in multiple columns and the same condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM