简体   繁体   English

python pandas将字典转换为数据框

[英]python pandas convert a dictionary to a dataframe

i have two dictionaries as follows. 我有两个字典如下。 I can convert the first to a dataframe , but the second gives error. 我可以将第一个转换为dataframe,但第二个会出错。 Why? 为什么?

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

raw_data1 = {
        'subject_id': 3,
        'first_name': 4, 
        'last_name': 7}
raw_data1
dfz = pd.DataFrame(raw_data1 )

This is happening because you are not passing an index, which is required when using scalar values. 发生这种情况是因为您没有传递索引,而使用标量值时却需要传递索引。 So to solve your issue you would do: 因此,要解决您的问题,您可以执行以下操作:

pd.DataFrame(raw_data1, index=[0])

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

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