从字典创建Python DataFrame,其中键是列名,值构成行 [英] Create Python DataFrame from dictionary where keys are the column names and values form the row

查看:50
本文介绍了从字典创建Python DataFrame,其中键是列名,值构成行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉python,但对panda DataFrames还是陌生的.我有一本这样的字典:

I am familiar with python but new to panda DataFrames. I have a dictionary like this:

a={'b':100,'c':300}

我想将其转换为DataFrame,其中b和c是列名,第一行是100,300(b在b之下,而300在c之下).我想要一个可以推广到更长的字典,包含更多项目的解决方案.谢谢!

And I would like to convert it to a DataFrame, where b and c are the column names, and the first row is 100,300 (100 is underneath b and 300 is underneath c). I would like a solution that can be generalized to a much longer dictionary, with many more items. Thank you!

推荐答案

将值作为列表传递:

a={'b':[100,],'c':[300,]}
pd.DataFrame(a)

     b    c
0  100  300

或者如果由于某种原因您不想使用列表,请添加索引:

Or if for some reason you don't want to use a list, include an index:

a={'b':100,'c':300}
pd.DataFrame(a, index=['i',])

     b    c
i  100  300

这篇关于从字典创建Python DataFrame,其中键是列名,值构成行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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