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

时间:2019-02-23 12:31:16

标签: python pandas dataframe

我对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数据框?

1 个答案:

答案 0 :(得分:1)

首先将其转换为列表,然后将其传递给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