将字典转换为pandas数据框 [英] Convert a dictionary to a pandas dataframe

查看:141
本文介绍了将字典转换为pandas数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将仅具有1条记录的字典转换为pandas数据框.我从其他解决方案中使用了以下代码:

I'm trying to convert a dictionary that only has 1 record to a pandas dataframe. I've used the following code from other solutions:

d =  {'id': 'CS2_056', 'cost': 2, 'name': 'Tap'}

pd.DataFrame(d.items(), columns=['id', 'cost','name'])

但是出现以下错误:

PandasError: DataFrame constructor not properly called!

推荐答案

您的字典只有一个记录使用列表:

You dict has only one record use list:

import pandas as pd
d =  {'id': 'CS2_056', 'cost': 2, 'name': 'Tap'}
df = pd.DataFrame([d], columns=d.keys())
print df

输出:

   id  cost name
0  CS2_056     2  Tap

这篇关于将字典转换为pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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