简体   繁体   English

将'records'参数传递给Pandas to_dict方法

[英]Passing 'records' parameter to Pandas to_dict method

I have a dataframe that I'd like to convert to a dictionary. 我有一个要转换为字典的数据框。 I want each column in the dataframe to be mapped to the values in it. 我希望将数据框中的每一列映射到其中的值。

import numpy as np 
import pandas as pd 

data = np.random.randn(5,5)
df = pd.DataFrame(data) 
dict1 = df.to_dict(outtype = 'records') 

Here is the error I get: 这是我得到的错误:

Value Error: outtype records not understood 值错误:输出类型记录不被理解

However, when I check the docs for this method, records is listed as a valid parameter. 但是,当我检查此方法的文档时 ,记录被列为有效参数。 Moreover, I am able to convert my dataframe to all the other dictionary formats mentioned in docs (list, series, and dict). 此外,我能够将数据框转换为文档中提到的所有其他字典格式(列表,系列和字典)。 I'm not sure what could be going wrong. 我不确定会出什么问题。

尝试这个:

dict1 = df.to_dict('records')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM