Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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_Pandas_Dataframe - Fatal编程技术网

Python数据帧:根据其余部分添加列

Python数据帧:根据其余部分添加列,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据帧,我想使用我要创建的“键”将它们连接起来。 我的数据帧的形式如下: Column1 Column2 Column3 1 240 31-02-16 2 350 25-03-16 3 100 31-03-16 4 500 13-02-16 我想创建一个新列,如下所示: str(第1列)+“-”+str(第2列)+“-”+str(第3列) 你知道怎么做

我有两个数据帧,我想使用我要创建的“键”将它们连接起来。 我的数据帧的形式如下:

Column1   Column2   Column3  
   1        240     31-02-16  
   2        350     25-03-16  
   3        100     31-03-16  
   4        500     13-02-16
我想创建一个新列,如下所示:

str(第1列)+“-”+str(第2列)+“-”+str(第3列)


你知道怎么做吗?

最简单的是每列将其转换为
字符串:

df['new'] = df.Column1.astype(str) + "_" + 
            df.Column2.astype(str) + "_" + 
            df.Column3.astype(str)
print (df)
   Column1  Column2   Column3             new
0        1      240  31-02-16  1_240_31-02-16
1        2      350  25-03-16  2_350_25-03-16
2        3      100  31-03-16  3_100_31-03-16
3        4      500  13-02-16  4_500_13-02-16
具有应用程序的解决方案:

df['new']=df[['Column1','Column2','Column3']].apply(lambda x: '_'.join(x.astype(str)),axis=1)
print (df)
   Column1  Column2   Column3             new
0        1      240  31-02-16  1_240_31-02-16
1        2      350  25-03-16  2_350_25-03-16
2        3      100  31-03-16  3_100_31-03-16
3        4      500  13-02-16  4_500_13-02-16

最简单的是通过每列转换为
字符串

df['new'] = df.Column1.astype(str) + "_" + 
            df.Column2.astype(str) + "_" + 
            df.Column3.astype(str)
print (df)
   Column1  Column2   Column3             new
0        1      240  31-02-16  1_240_31-02-16
1        2      350  25-03-16  2_350_25-03-16
2        3      100  31-03-16  3_100_31-03-16
3        4      500  13-02-16  4_500_13-02-16
具有应用程序的解决方案:

df['new']=df[['Column1','Column2','Column3']].apply(lambda x: '_'.join(x.astype(str)),axis=1)
print (df)
   Column1  Column2   Column3             new
0        1      240  31-02-16  1_240_31-02-16
1        2      350  25-03-16  2_350_25-03-16
2        3      100  31-03-16  3_100_31-03-16
3        4      500  13-02-16  4_500_13-02-16