Python 通过将两个字符串列合并在一起创建新列
希望在数据帧中将两个字符串列合并为一个新列 例如—Python 通过将两个字符串列合并在一起创建新列,python,python-3.x,pandas,Python,Python 3.x,Pandas,希望在数据帧中将两个字符串列合并为一个新列 例如— >>> df = pd.DataFrame({'Primary Type':['a','b','c'],'Description':['1','2','3']}) >>> df Primary Type Description 0 a 1 1 b 2 2 c 3 我希望输出是
>>> df = pd.DataFrame({'Primary Type':['a','b','c'],'Description':['1','2','3']})
>>> df
Primary Type Description
0 a 1
1 b 2
2 c 3
我希望输出是
Primary Type Description combined
0 a 1 a ,1
1 b 2 b ,2
2 c 3 c ,3
这是我们尝试过的-
df['combined'] = df['Primary Type'] + ', ' + df['Description']
但这似乎不起作用
其他想法?在转换为str后使用join缩短代码
df['combined'] = df['Primary Type'].map(str) + ' ,' +
df['Description'].map(str)
df
Primary Type Description combined
a 1 a ,1
b 2 b ,2
c 3 c ,3
我刚试过你的代码,你试过的对我来说很有用。为什么它似乎对你不起作用?
df['New']=df.astype(str).apply(','.join,1)
df
Primary Type Description New
0 a 1 a,1
1 b 2 b,2
2 c 3 c,3