问题描述
我已经看过以下问题: pandas创建命名列在字典数据框中.但是,我的示例略有不同.
I looked already at this question: pandas create named columns in dataframe from dict. However, my example is slightly different.
我有一本字典:my_dict = {'key1' : [1,2,3], 'key2' : [4,5,6], 'key3' :[7,8,9]}
然后我创建了一个熊猫数据框:df = pd.DataFrame.from_dict(my_dict, orient='index')
,它是面向行的.但是,编写columns = ['one', 'two', 'three']
时出现错误,如上面的链接所示.
And I created a pandas dataframe: df = pd.DataFrame.from_dict(my_dict, orient='index')
, which is row oriented. However, when writing columns = ['one', 'two', 'three']
I get an error, as in the link above.
如何命名?
推荐答案
您是否有理由无法在下一行设置列名称?
Is there a reason you can't set the column names on the next line?
my_dict = {'key1' : [1,2,3], 'key2' : [4,5,6], 'key3' :[7,8,9]}
df = pd.DataFrame.from_dict(my_dict, orient='index')
df.columns = ['one', 'two', 'three']
应该工作.
这篇关于使用Orient ='index'在Pandas数据框from_dict中设置列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!