gpt4 book ai didi

python - 仅将指定的列从列表列表传递给 Pandas 数据框

转载 作者:行者123 更新时间:2023-12-04 10:00:45 25 4
gpt4 key购买 nike

我在 Python 中有以下列表

myList = [datetime.datetime(2020, 5, 16, 0, 15), '9387.95000000', '9418.96000000', '9386.22000000', '9406.76000000', '138.33460500', 1589581199999, '1300408.00793448', 2156, '88.84298900', '835174.15438128', '0'], [datetime.datetime(2020, 5, 16, 0, 20), '9407.03000000', '9437.65000000', '9405.96000000', '9429.36000000', '190.78911000', 1589581499999, '1797685.31006086', 2454, '100.00883200', '942460.13231483', '0', ...]

我正在尝试从中创建一个 Pandas 数据框,这是我尝试过的:
df = pd.DataFrame(Result, columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume'])

这会给我以下错误:
AssertionError: 6 columns passed, passed data had 12 columns

如何仅将列表列表的前 6 个元素传递给数据框,以免出现错误?提前致谢!

最佳答案

过滤列表理解中的前 6 个值:

df = pd.DataFrame([x[:6] for x in myList], 
columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume'])
print (df)
Date Open High Low \
0 2020-05-16 00:15:00 9387.95000000 9418.96000000 9386.22000000
1 2020-05-16 00:20:00 9407.03000000 9437.65000000 9405.96000000

Close Volume
0 9406.76000000 138.33460500
1 9429.36000000 190.78911000

关于python - 仅将指定的列从列表列表传递给 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61835018/

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