pandas :从dict在DataFrame中创建命名列 [英] Pandas: create named columns in DataFrame from dict
本文介绍了 pandas :从dict在DataFrame中创建命名列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以下形式的字典对象:
I have a dictionary object of the form:
my_dict = {id1: val1, id2: val2, id3: val3, ...}
我想将其创建到DataFrame中,并在其中将2列命名为"business_id"和"business_code".
I want to create this into a DataFrame where I want to name the 2 columns 'business_id' and 'business_code'.
我尝试过:
business_df = DataFrame.from_dict(my_dict,orient='index',columns=['business_id','business_code'])
但是它说from_dict
不接受column参数.
But it says from_dict
doesn't take in a columns argument.
TypeError:from_dict()得到了意外的关键字参数'columns'
TypeError: from_dict() got an unexpected keyword argument 'columns'
推荐答案
您可以遍历以下项目:
In [11]: pd.DataFrame(list(my_dict.iteritems()),
columns=['business_id','business_code'])
Out[11]:
business_id business_code
0 id2 val2
1 id3 val3
2 id1 val1
这篇关于 pandas :从dict在DataFrame中创建命名列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文