有没有一种方法可以将字典作为python中pandas Dataframe的条目? [英] Is there a way to have a dictionary as an entry of a pandas Dataframe in python?
本文介绍了有没有一种方法可以将字典作为python中pandas Dataframe的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
类似:
d={'a':1, 'b'=2}
data=pandas.DataFrame()
data['new column'] = d
data['new column'][0]
最后一条命令将返回字典d的位置?
where the last command will return the dictionary 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}
这篇关于有没有一种方法可以将字典作为python中pandas Dataframe的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文