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的解决方案…相同的概念这是完美的