如何在一个熊猫数据框中合并/合并多个文本列 而不是将各个列单独串联
例如
d = pd.DataFrame({'A':['one','two','three'],'B':['London','New York','Tokyo'],'C':['UK','USA','Japan'],'D':['Hot','Hot','Cold']})
输出应为
0 one London UK Hot
1 two New York USA Hot
2 three Tokyo Japan Cold
Name: combined, dtype: object
答案 0 :(得分:1)
将axis=1
与join
一起使用DataFrame.apply
:
d.apply(' '.join, axis=1)
0 one London UK Hot
1 two New York USA Hot
2 three Tokyo Japan Cold
dtype: object