gpt4 book ai didi

python - 将列表放入数据框中时列传递错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:02 26 4
gpt4 key购买 nike

我有这个列表:

20161216014500
20161216020000
20161216021500
20161216023000
20161216024500
20161216030000
20161216031500
20161216033000
20161216034500
20161216040000
20161216041500
20161216043000
20161216044500
20161216050000
20161216051500
20161216053000
20161216054500

我想在解析它并通过这段代码将其放入正确的格式之后:

for row in rows:
if "".join(row).strip() != "":
chaine = str(row[0]+row[1])
date = chaine[:10] + " " + chaine[11:]
header = parseDate(date)
header = str(header).replace('-','')
header = str(header).replace(':','')
header = str(header).replace(' ','')
print header

我想使用 pandas 在数据框中插入标题(上面的列表):

newDataframe = pd.DataFrame(data, index=index, columns=header)

这是我得到的错误:

 14 columns passed, passed data had 1 columns

此错误的原因是什么以及如何更正?

最佳答案

你可以用这种方式做同样的事情:

import pandas as pd
rows = ['20161216014500',
'20161216020000',
'20161216021500',
'20161216023000',
'20161216024500',
'20161216030000',
'20161216031500',
'20161216033000',
'20161216034500',
'20161216040000',
'20161216041500',
'20161216043000',
'20161216044500',
'20161216050000',
'20161216051500',
'20161216053000',
'20161216054500']

df = pd.DataFrame(rows, columns=['date'])

pd.to_datetime(df['date'], format='%Y%m%d%H%M%S')

df

输出:

    date
0 20161216014500
1 20161216020000
2 20161216021500
3 20161216023000
4 20161216024500
5 20161216030000
6 20161216031500
7 20161216033000
8 20161216034500
9 20161216040000
10 20161216041500
11 20161216043000
12 20161216044500
13 20161216050000
14 20161216051500
15 20161216053000
16 20161216054500

关于python - 将列表放入数据框中时列传递错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41461667/

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