列(以索引格式)到数据帧?

时间:2016-10-26 21:53:18

标签: python pandas dataframe data-cleaning

我的数据框中有一个列格式为索引:

0      [u'Basketball', u'Swimming', u'Gym']
1      [u'Gym', u'Soccer', u'Football']
2      [u'Ballet', u'Basketball', u'Volleyball']

我有一个简单的方法来清理它(删除你和方括号)然后用(',')拆分它们,以便将运动分组到三列?

1 个答案:

答案 0 :(得分:2)

考虑s

s = pd.Series([
      "[u'Basketball', 'Swimming', 'Gym']",
      "[u'Gym', u'Soccer', u'Football']",
      "[u'Ballet', u'Basketball', u'Volleyball']"
    ])
s

0           [u'Basketball', 'Swimming', 'Gym']
1             [u'Gym', u'Soccer', u'Football']
2    [u'Ballet', u'Basketball', u'Volleyball']
dtype: object

最快的方法是apply eval

s.apply(eval)

0         [Basketball, Swimming, Gym]
1             [Gym, Soccer, Football]
2    [Ballet, Basketball, Volleyball]
dtype: object

获取数据框

s.apply(eval).apply(pd.Series)

enter image description here