[英]Converting a dictionary including lists with different lengths to a 2-column dataframe
I have a dictionary like this one我有一本像这样的字典
my_dict = {0:[1,2],
1:[1, 5, 100,120],
2:[1, 89, 90, 1625, 98, 0, 10]}
I want to convert it to a data frame with just two columns like this one.我想将它转换为只有两列这样的数据框。
col1 col2
0 [1, 2]
1 [1, 5, 100,120]
2 [1, 89, 90, 1625, 98, 0, 10]
The second column includes a list of numbers.第二列包括一个数字列表。 Any suggestions?
有什么建议么?
#Input dictionary
my_dict = {0:[1,2],
1:[1, 5, 100,120],
2:[1, 89, 90, 1625, 98, 0, 10]}
#Convert dictionary to dataframe
df = pd.DataFrame(my_dict.items(),columns=["col1","col2"])
print(df)
Output: Output:
col1 col2
0 0 [1, 2]
1 1 [1, 5, 100, 120]
2 2 [1, 89, 90, 1625, 98, 0, 10]
s=pd.Series(my_dict).reset_index()
Out[32]:
index 0
0 0 [1, 2]
1 1 [1, 5, 100, 120]
2 2 [1, 89, 90, 1625, 98, 0, 10]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.