将包含值列表的字典转换为数据框 [英] Converting a dictionary with lists for values into a dataframe

查看:167
本文介绍了将包含值列表的字典转换为数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一段时间浏览SO,看来我有一个独特的问题.

I spent a while looking through SO and seems I have a unique problem.

我有一本类似于以下内容的字典:

I have a dictionary that looks like the following:

dict={
    123: [2,4],
    234: [6,8],
    ...
}

我要将包含值列表的字典转换为3列数据框,如下所示:

I want to convert this dictionary that has lists for values into a 3 column data frame like the following:

time, value1, value2
123, 2, 4
234, 6, 8
...

我可以跑步:

pandas.DataFrame(dict)

但这会生成以下内容:

123, 234, ...
2, 6, ...
4, 8, ...

可能是一个简单的解决方法,但我仍在捡熊猫

Probably a simple fix but I'm still picking up pandas

推荐答案

您可以按照levi的建议对数据进行预处理,也可以在创建数据框后对其进行转置.

You can either preprocess the data as levi suggests, or you can transpose the data frame after creating it.

testdict={
    123: [2,4],
    234: [6,8],
    456: [10, 12]
}
df = pd.DataFrame(testdict)
df = df.transpose()

print(df)
#      0  1
# 123  2  4
# 234  6  8

这篇关于将包含值列表的字典转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆