gpt4 book ai didi

python - 将字符串/列表转换为数据框

转载 作者:行者123 更新时间:2023-12-01 08:18:32 25 4
gpt4 key购买 nike

我对 Python 有点陌生。我有多个列表,如下所示:

数据:

(5300,
53,
1290,
'`Tiytr`',
'`professional`',
1,
3,
datetime.datetime(2018, 10, 13, 0, 0),
datetime.datetime(2018, 9, 17, 0, 0))

他们的商店如下:

((63, 65, 1200, '`Jsalem`', '`professional`', 2, 1, datetime.datetime(2018, 10, 13, 0, 0), datetime.datetime(2015, 10, 19, 0, 0)), (70, 71, 1175, '`Cme`', '`professional`', 1, 0, datetime.datetime(2018, 10, 13, 0, 0), datetime.datetime(2018, 10, 12, 0, 0)), (90, 55, 1100, '`Jerusalem`', '`professional`', 2, 1, datetime.datetime(2018, 10, 13, 0, 0), datetime.datetime(2015, 10, 20, 0, 0))

如何将它们转换为 pandas 数据框?

最佳答案

首先将其转换为列表,然后将其传递给pandas.DataFrame

import pandas as pd 
import datetime

x = ((63, 65, 1200, '`Jsalem`', '`professional`', 2, 1, datetime.datetime(2018, 10, 13, 0, 0), datetime.datetime(2015, 10, 19, 0, 0)),
(70, 71, 1175, '`Cme`', '`professional`', 1, 0, datetime.datetime(2018, 10, 13, 0, 0), datetime.datetime(2018, 10, 12, 0, 0)),
(90, 55, 1100, '`Jerusalem`', '`professional`', 2, 1, datetime.datetime(2018, 10, 13, 0, 0), datetime.datetime(2015, 10, 20, 0, 0)))

df = pd.DataFrame(list(x))
print(df)

输出:

   0   1     2            3               4  5  6          7          8                                                                       
0 63 65 1200 `Jsalem` `professional` 2 1 2018-10-13 2015-10-19
1 70 71 1175 `Cme` `professional` 1 0 2018-10-13 2018-10-12
2 90 55 1100 `Jerusalem` `professional` 2 1 2018-10-13 2015-10-20

关于python - 将字符串/列表转换为数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54841638/

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