splitting a column into multiple columns with specific name in pandas dataframe

前端 未结 2 1703
你的背包
你的背包 2020-12-30 10:00

I have following dataframe:

pri    sec
TOM    AB,CD,EF
JACK   XY,YZ
HARRY  FG
NICK   KY,NY,SD,EF,FR

I need following output with column nam

2条回答
  •  孤城傲影
    2020-12-30 10:33

    Use join + split + add_prefix:

    df = df.join(df['sec'].str.split(',', expand=True).add_prefix('sec'))
    print (df)
         pri             sec sec0  sec1  sec2  sec3  sec4
    0    TOM        AB,CD,EF   AB    CD    EF  None  None
    1   JACK           XY,YZ   XY    YZ  None  None  None
    2  HARRY              FG   FG  None  None  None  None
    3   NICK  KY,NY,SD,EF,FR   KY    NY    SD    EF    FR
    

    And if need NaNs add fillna:

    df = df.join(df['sec'].str.split(',', expand=True).add_prefix('sec').fillna(np.nan))
    print (df)
         pri             sec sec0 sec1 sec2 sec3 sec4
    0    TOM        AB,CD,EF   AB   CD   EF  NaN  NaN
    1   JACK           XY,YZ   XY   YZ  NaN  NaN  NaN
    2  HARRY              FG   FG  NaN  NaN  NaN  NaN
    3   NICK  KY,NY,SD,EF,FR   KY   NY   SD   EF   FR
    

提交回复
热议问题