Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过将两个字符串列合并在一起创建新列_Python_Python 3.x_Pandas - Fatal编程技术网

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