【发布时间】:2018-11-17 22:56:51
【问题描述】:
我有一个如下所示的数据框:
name val
0 cat ['Furry: yes', 'Fast: yes', 'Slimy: no', 'Living: yes']
1 dog ['Furry: yes', 'Fast: yes', 'Slimy: no', 'Living: yes']
2 snail ['Furry: no', 'Fast: no', 'Slimy: yes', 'Living: yes']
3 paper ['Furry: no', 'Fast: no', 'Slimy: no', 'Living: no']
对于 val 列中列表中的每个项目,我想在 ':' 分隔符上拆分项目。然后我想让 item[0] 成为列名,而 item[1] 成为该特定列的值。像这样:
name Furry Fast Slimy Living
0 cat yes yes no yes
1 dog yes yes no yes
2 snail no no yes yes
3 paper no no no no
我尝试将 apply(pd.Series) 用于 val 列,但这仍然给我留下了许多列,我必须手动进行拆分,或者弄清楚如何迭代地遍历所有列并进行拆分。我更喜欢从零开始拆分并创建列名。知道如何实现这一目标吗?
【问题讨论】:
标签: python list pandas dataframe split