Pandas 从列表列表中一次添加多个新列

Aus*_*tin 4 list multiple-columns pandas python-3.5

我有一个时间戳列表列表,其中每个内部列表如下所示:

['Tue', 'Feb', '7', '10:07:40', '2017']

Pandas 是否可以将五个新列同时添加到已创建的数据帧(与外部列表长度相同),这些列等于这些值中的每一个,名称为'day','month','date','time','year'

jez*_*ael 6

我认为你可以使用DataFrame构造函数concat

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9]})

L = [['Tue', 'Feb', '7', '10:07:40', '2017'],
     ['Tue', 'Feb', '7', '10:07:40', '2017'],
     ['Tue', 'Feb', '7', '10:07:40', '2017']]

cols = ['day','month','date','time','year']
df1 = pd.DataFrame(L, columns=cols)
print (df1)
   day month date      time  year
0  Tue   Feb    7  10:07:40  2017
1  Tue   Feb    7  10:07:40  2017
2  Tue   Feb    7  10:07:40  2017

df2 = pd.concat([df, df1], axis=1)
print (df2)
   A  B  C  day month date      time  year
0  1  4  7  Tue   Feb    7  10:07:40  2017
1  2  5  8  Tue   Feb    7  10:07:40  2017
2  3  6  9  Tue   Feb    7  10:07:40  2017
Run Code Online (Sandbox Code Playgroud)

一个班轮:

df2 = pd.concat([df, pd.DataFrame(L, columns=['day','month','date','time','year'])], axis=1)
print (df2)
   A  B  C  day month date      time  year
0  1  4  7  Tue   Feb    7  10:07:40  2017
1  2  5  8  Tue   Feb    7  10:07:40  2017
2  3  6  9  Tue   Feb    7  10:07:40  2017
Run Code Online (Sandbox Code Playgroud)