简体   繁体   English

如何将两个不同列中的唯一值复制到第三列

[英]how to copy unique values from two different columns into third column

i am trying to copy unique values from two differnt columns.我正在尝试从两个不同的列中复制唯一值。

Your question is confusing.你的问题令人困惑。 But if you want to check if col A == Col B AND col C == col D and then copy the value to a colD, here is what you can do:但是,如果您想检查 col A == Col B AND col C == col D,然后将值复制到 colD,您可以执行以下操作:

df
FirstName   SecondName  ID1 ID2
    ABC     ABC         2   2
    DFH     GGG         2   2
    HGG     UUU         2   2
    NNN     NNN         3   3

Here is a list comprehension to store the logic and capture the values.这是存储逻辑和捕获值的列表理解。 If not equal, it will generate a NaN value, that you could change to 0 or '-' or whatever...如果不相等,它将生成一个 NaN 值,您可以将其更改为 0 或 '-' 或其他...

id3 = [df.ID1[i] if (df.FirstName[i] == df.SecondName[i]) and (df.ID1[i] == df.ID2[i]) else 'NaN' for i in range(df.shape[0])]

Create the new column:创建新列:

df['ID3'] = id3

Print the resulting df.打印结果df。 It only copies the number when FirstName = SecondName AND ID1 = ID2它仅在 FirstName = SecondName AND ID1 = ID2 时复制数字

    FirstName   SecondName  ID1 ID2 ID3
    ABC          ABC        2   2   2
    DFH          GGG        2   2   NaN
    HGG          UUU        2   2   NaN
    NNN          NNN        3   3   3

暂无
暂无

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

相关问题 groupby 两列并计算第三列中的唯一值 - groupby two columns and count unique values from a third column 如何根据 pandas 中两个不同列的条件将值从一列复制到另一列? - how to copy values from one column into another column based on conditions of two different columns in pandas? 如何将 DataFrame 按两列分组,并使用第三列的最小值和最大值创建两个新列? - How to group DataFrame by two columns and create two new columns with min and max values from third column? 根据第三列中的条件获取两个不同列的值 - Get values of two different columns based on a condition in a third column 熊猫:当其他两列是唯一对时,从一列复制值 - Pandas: Copy values from one column when two other columns are unique pairs 如果两个不同数据帧中两列的值匹配,则将df2中另一列的值复制到df1中的列 - If values from two columns in two different data frames match then copy values from another column in df2 to column in df1 Pandas 比较不同数据帧的两列,如果匹配,则复制第三列的值 - Pandas compare two columns of different dataframes and copy value of a third column if there is a match 用值分组两列以获得第三列 - Grouping two columns with values to get a third column 两个如何组合两列不同的数据帧,使它们具有唯一的值? - How two combine two columns of different dataframes such that they have unique values? 组合来自两个不同列的两个值并打印唯一值和唯一值计数 - combining two values from two different columns and print unique values and count of unique values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM