将“记录”参数传递给Pandas to_dict方法 [英] Passing 'records' parameter to Pandas to_dict method

查看:387
本文介绍了将“记录”参数传递给Pandas to_dict方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想转换成字典的数据框。我想要将数据帧中的每一列映射到其中的值。

  import numpy as np 
import pandas as pd

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

这是我得到的错误:


值错误:outtype记录不明白


但是,当我查看文档对于该方法,记录被列为有效参数。此外,我可以将我的数据框转换为docs(list,series和dict)中提到的所有其他字典格式。我不知道会发生什么问题。

尝试这样:

  dict1 = df.to_dict('records')


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.

解决方案

Try this:

dict1 = df.to_dict('records')

这篇关于将“记录”参数传递给Pandas to_dict方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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