简体   繁体   English

有没有一种方法可以将字典作为python中的熊猫数据框的入口?

[英]Is there a way to have a dictionary as an entry of a pandas Dataframe in python?

Something like: 就像是:

d={'a':1, 'b'=2}
data=pandas.DataFrame()
data['new column'] = d
data['new column'][0]

where the last command will return the dictionary d? 最后一条命令将在哪里返回字典d?

You can wrap the dictionary in a list, so that the dictionary will be treated as an element instead of an iterable: 您可以将字典包装在列表中,以便将字典视为元素而不是可迭代的:

d={'a':1, 'b': 2}
data=pd.DataFrame()
data['new column'] = [d]
data['new column'][0]

# {'a': 1, 'b': 2}

Convert it to string, in case you need to store in a db. 将其转换为字符串,以防您需要存储在数据库中。 If you wrap it in a list, you won't be able to query it. 如果将其包装在列表中,将无法查询它。

dic={'key':1}
df=pd.DataFrame()
df['a'][1] = str(dic)

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

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