Pandas split column of lists into multiple columns

后端 未结 8 1675
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 06:28

I have a pandas DataFrame with one column:

import pandas as pd

df = pd.DataFrame(
    data={
        \"teams\": [
            


        
8条回答
  •  面向向阳花
    2020-11-21 07:15

    Here's another solution using df.transform and df.set_index:

    >>> (df['teams']
           .transform([lambda x:x[0], lambda x:x[1]])
           .set_axis(['team1','team2'],
                      axis=1,
                      inplace=False)
        )
    
      team1 team2
    0    SF   NYG
    1    SF   NYG
    2    SF   NYG
    3    SF   NYG
    4    SF   NYG
    5    SF   NYG
    6    SF   NYG
    

提交回复
热议问题