gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:36:33 25 4
gpt4 key购买 nike

我有一个时间戳列表列表,其中每个内部列表如下所示:
['Tue', 'Feb', '7', '10:07:40', '2017']
Pandas 是否可以同时将五个新列添加到已经创建的数据帧(与外部列表长度相同),这些列等于这些值中的每一个,名称为 'day','month','date','time','year' ?

最佳答案

我想你可以用 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

一个类轮:
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

关于list - Pandas 从列表列表中一次添加多个新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43198105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com