在《熊猫》中,我有一本字典,它看起来像下面的一本:
{'Anemones & allies': ['Carnivore'],
'Ants, bees & wasps': ['Omnivore', 'Herbivore', 'Nectar', 'Insects', 'Parasite'],
'Beetles & bugs': ['Herbivore', 'Carnivore', 'Nectar', 'Insects'],
'Birds': ['Carnivore'],
'Fishes': ['Carnivore', 'Plankton or Particles']}
我想把它转换成一个数据帧,在这个框架里你可以看到动物类型可能吃什么。因此它看起来与下图类似:
当我试图生成这样一个表时,我有一种不正确的感觉,因为我需要相当多的代码行。所以我的问题是,有没有一个很好的函数将这个字典映射到一个数据帧,这样它看起来像上表中的内容?在
从
dict
(d)创建DataFrame
,然后使用get_dummies
最简单的方法
使用
pd.str.get_dummies
更复杂
^{pr2}$但可能是推荐的
相关问题 更多 >
编程相关推荐