Splitting multiple columns into rows in pandas dataframe

后端 未结 5 1738
独厮守ぢ
独厮守ぢ 2020-12-03 09:16

I have a pandas dataframe as follows:

ticker    account      value         date
aa       assets       100,200       20121231, 20131231
bb       liabilities           


        
5条回答
  •  温柔的废话
    2020-12-03 09:35

    You can first split columns, create Series by stack and remove whitespaces by strip:

    s1 = df.value.str.split(',', expand=True).stack().str.strip().reset_index(level=1, drop=True)
    s2 = df.date.str.split(',', expand=True).stack().str.strip().reset_index(level=1, drop=True)
    

    Then concat both Series to df1:

    df1 = pd.concat([s1,s2], axis=1, keys=['value','date'])
    

    Remove old columns value and date and join:

    print (df.drop(['value','date'], axis=1).join(df1).reset_index(drop=True))
      ticker      account value      date
    0     aa       assets   100  20121231
    1     aa       assets   200  20131231
    2     bb  liabilities    50  20141231
    3     bb  liabilities   150  20131231
    

提交回复
热议问题