我正在尝试合并同一数据框中的列(列 B 和 C)的值。B 和 C 有时具有相同的值。B 中的某些值存在于 C 中,而 C 中的某些值存在于 B 中。最终结果将显示一列是两列的组合。
A B C D
Apple Canada '' RED
Bananas '' Germany BLUE
Carrot US US GREEN
Dorito '' '' INDIGO
A B C
Apple Canada RED
Bananas Germany BLUE
Carrot US GREEN
Dorito '' INDIGO
您可以对字符串进行排序并取最后一个:
df['B'] = df[['B', 'C']].apply(lambda x: x.sort_values()[1], axis=1)
df=df.drop('C', 1).rename(columns={'D':'C'})
print(df)
输出:
A B C
0 Apple Canada RED
1 Bananas Germany BLUE
2 Carrot US GREEN
3 Dorito '' INDIGO
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句