use*_*161 2 python dictionary dataframe pandas
在熊猫中,我有一本字典,如下所示:
{'Anemones & allies': ['Carnivore'],
'Ants, bees & wasps': ['Omnivore', 'Herbivore', 'Nectar', 'Insects', 'Parasite'],
'Beetles & bugs': ['Herbivore', 'Carnivore', 'Nectar', 'Insects'],
'Birds': ['Carnivore'],
'Fishes': ['Carnivore', 'Plankton or Particles']}
Run Code Online (Sandbox Code Playgroud)
我想把它转换成一个数据帧,你可以在其中看到动物类型可能吃什么。所以它看起来类似于下图:
在尝试生成这样的表格时,我觉得我的做法不正确,因为我需要相当多的代码行。所以我的问题是,是否有一个很好的函数可以将这个字典映射到一个 DataFrame 使它看起来像上表?
最简单的
使用方法pd.str.get_dummies
dct = {
'Anemones & allies': ['Carnivore'],
'Ants, bees & wasps': ['Omnivore', 'Herbivore', 'Nectar', 'Insects', 'Parasite'],
'Beetles & bugs': ['Herbivore', 'Carnivore', 'Nectar', 'Insects'],
'Birds': ['Carnivore'],
'Fishes': ['Carnivore', 'Plankton or Particles']
}
pd.Series(dct).str.join('|').str.get_dummies()
Carnivore Herbivore Insects Nectar Omnivore Parasite Plankton or Particles
Anemones & allies 1 0 0 0 0 0 0
Ants, bees & wasps 0 1 1 1 1 1 0
Beetles & bugs 1 1 1 1 0 0 0
Birds 1 0 0 0 0 0 0
Fishes 1 0 0 0 0 0 1
Run Code Online (Sandbox Code Playgroud)
更复杂
但可能推荐
from sklearn.preprocessing import MultiLabelBinarizer
dct = {
'Anemones & allies': ['Carnivore'],
'Ants, bees & wasps': ['Omnivore', 'Herbivore', 'Nectar', 'Insects', 'Parasite'],
'Beetles & bugs': ['Herbivore', 'Carnivore', 'Nectar', 'Insects'],
'Birds': ['Carnivore'],
'Fishes': ['Carnivore', 'Plankton or Particles']
}
s = pd.Series(dct)
mlb = MultiLabelBinarizer()
d = mlb.fit_transform(s)
c = mlb.classes_
pd.DataFrame(d, s.index, c)
Carnivore Herbivore Insects Nectar Omnivore Parasite Plankton or Particles
Anemones & allies 1 0 0 0 0 0 0
Ants, bees & wasps 0 1 1 1 1 1 0
Beetles & bugs 1 1 1 1 0 0 0
Birds 1 0 0 0 0 0 0
Fishes 1 0 0 0 0 0 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
880 次 |
| 最近记录: |