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