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

column (in index format) to dataframe?

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

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)