我的数据框中有一列的格式类似于索引:

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


我是否有一种简单的方法可以清理(删除u和方括号),然后用(',')拆分它们,以便将运动分为三列?

最佳答案

考虑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)


python - 列(以索引格式)到数据框?-LMLPHP

10-06 01:10