[英]Python Convert a list into a dataframe
I have a list that consists of lists.我有一个由列表组成的列表。 Looks something like this:看起来像这样:
[[1,2,3,4,5],['john','leo','steve','ben','sally'],['22','55','66','11','33'],['blue','green','red','yellow','pink']]
I want to convert this into a dataframe that looks like this:我想将其转换为如下所示的 dataframe:
1 2 3 4 5
0 john leo steve ben sally
1 22 55 66 11 33
2 blue green red yellow pink
Any suggestions?有什么建议么?
I tried to play around with the reshape function, but I can't get it to work.我试图玩弄重塑 function,但我无法让它工作。
I also tried looking at ValueError: Shape of passed values is (1, 6), indices imply (6, 6) but that didn't help either.我还尝试查看ValueError:传递值的形状是 (1, 6),索引意味着 (6, 6)但这也无济于事。
You could do:你可以这样做:
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)
Output Output
1 2 3 4 5
0 john leo steve ben sally
1 22 55 66 11 33
2 blue green red yellow pink
How about:怎么样:
pd.DataFrame(data[1:], columns=data[0])
Output: Output:
1 2 3 4 5
0 john leo steve ben sally
1 22 55 66 11 33
2 blue green red yellow pink
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.