Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将列表转换为数据帧_Python_Python 3.x_Pandas - Fatal编程技术网

Python将列表转换为数据帧

Python将列表转换为数据帧,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个由列表组成的列表。 看起来像这样: [[1,2,3,4,5],['john','leo','steve','ben','sally'],['22','55','66','11','33'],['blue','green','red','yellow','pink']] 1 2 3 4 5 0 john leo steve ben sally 1 22 55 66 11 33 2 blue gre

我有一个由列表组成的列表。 看起来像这样:

[[1,2,3,4,5],['john','leo','steve','ben','sally'],['22','55','66','11','33'],['blue','green','red','yellow','pink']]
      1     2     3      4     5
0  john   leo steve    ben sally
1    22    55    66     11    33
2  blue green   red yellow  pink
我想将其转换为如下所示的数据帧:

[[1,2,3,4,5],['john','leo','steve','ben','sally'],['22','55','66','11','33'],['blue','green','red','yellow','pink']]
      1     2     3      4     5
0  john   leo steve    ben sally
1    22    55    66     11    33
2  blue green   red yellow  pink
有什么建议吗

我试着使用重塑功能,但无法使其工作

我也试着看了看,但也没用。

你可以做:

import pandas as pd


columns, *data = [[1,2,3,4,5],['john','leo','steve','ben','sally'],['22','55','66','11','33'],
                  ['blue','green','red','yellow','pink']]
df = pd.DataFrame(data=data, columns=columns)
print(df)
输出

      1      2      3       4      5
0  john    leo  steve     ben  sally
1    22     55     66      11     33
2  blue  green    red  yellow   pink
那么:

pd.DataFrame(data[1:], columns=data[0])
输出:

      1      2      3       4      5
0  john    leo  steve     ben  sally
1    22     55     66      11     33
2  blue  green    red  yellow   pink

pd.DataFrame(x[1:],columns=x[0])
如果x是列表-或者是Dani的解决方案…相同的概念这是完美的