Python Pandas:从 dict 在 DataFrame 中创建命名列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20340844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:14:48  来源:igfitidea点击:

Pandas: create named columns in DataFrame from dict

pythonpandastypeerrordataframe

提问by anonuser0428

I have a dictionary object of the form:

我有一个以下形式的字典对象:

my_dict = {id1: val1, id2: val2, id3: val3, ...}

I want to create this into a DataFrame where I want to name the 2 columns 'business_id' and 'business_code'.

我想将它创建到一个 DataFrame 中,我想在其中命名 2 列“business_id”和“business_code”。

I tried:

我试过:

business_df = DataFrame.from_dict(my_dict,orient='index',columns=['business_id','business_code'])

But it says from_dictdoesn't take in a columns argument.

但它说不from_dict接受列参数。

TypeError: from_dict() got an unexpected keyword argument 'columns'

类型错误:from_dict() 得到了一个意外的关键字参数“列”

采纳答案by Andy Hayden

You can iterate through the items:

您可以遍历项目:

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

回答by meyerson

Do this:

做这个:

create the dataframe

创建数据框

df = pd.DataFrame(data_as_2d_ndarray)

create a sorted list of column names from the dictionary - adjust the key karg as need to grab the sorting value from your dict, obvilous the dictionary the data must have consistent shapes

从字典中创建一个列名的排序列表 - 根据需要调整键 karg 以从字典中获取排序值,显然字典中的数据必须具有一致的形状

col_names = sorted(list(col_dict.iteritems()),key=lambda x:x[0])

reshape and set the column names

重塑并设置列名

df.columns  = zip(*col_names)[1]

回答by cfelix

To get the same functionality as the documentation and avoid using code workarounds, make sure you're using the most recent version of Pandas. I recently encountered the same error when running a line of code from the Pandas tutorial:

要获得与文档相同的功能并避免使用代码解决方法,请确保您使用的是最新版本的 Pandas。我最近在运行 Pandas 教程中的一行代码时遇到了同样的错误:

pd.DataFrame.from_dict(dict([('A', [1, 2, 3]), ('B', [4, 5, 6])]),orient='index', columns=['one', 'two', 'three'])

I checked the version of Pandas and found I was running version 22, when version 23 is available.

我检查了 Pandas 的版本,发现我运行的是 22 版本,此时版本 23 可用。

import pandas as pd
pd.__version__
Out[600]: '0.22.0'

I upgraded using pip:

我使用 pip 升级:

c:\pip install --upgrade pandas

I confirmed my version updated to 23, and the same from_dict() code worked without error. No code modifications required.

我确认我的版本更新到 23,并且相同的 from_dict() 代码没有错误。无需修改代码。

回答by Karthik Sunil

This is with respect to TypeError you faced. As per Pandas documentation, from_dict will take the keyword 'columns' only if the orient = 'index'.

这是关于您遇到的 TypeError 。根据 Pandas 文档,仅当 orient = 'index' 时 from_dict 才会采用关键字 'columns'。

回答by Ninjakannon

From version 0.23.0, you can specify a columnsparameter in from_dict:

从版本 0.23.0 开始,您可以在 中指定一个columns参数from_dict

my_dict = {id1: val1, id2: val2, id3: val3, ...}
prepared_dict = {i: x for i, x in enumerate(my_dict.items())}
df = pd.DataFrame.from_dict(prepared_dict, orient='index', columns=['business_id', 'business_code'])

Note: I also answered in kind on this similar question.

注意:我也对这个类似的问题进行了实物回答