Splitting multiple columns into rows in pandas dataframe

后端 未结 5 1745
独厮守ぢ
独厮守ぢ 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

    Because I'm too new, I'm not allowed to write a comment, so I write an "answer".

    @titipata your answer worked really good, but in my opinion there is a small "mistake" in your code I'm not able to find for my self.

    I work with the example from this question and changed just the values.

    df = pd.DataFrame([['title1', 'publisher1', '1.1,1.2', '1'],
                   ['title2', 'publisher2', '2', '2.1,2.2']],
                  columns=['titel', 'publisher', 'print', 'electronic'])
    
    explode(df, ['print', 'electronic'])
    
        publisher   titel   print   electronic
    0   publisher1  title1  1.1     1
    1   publisher1  title1  1.2     2.1
    2   publisher2  title2  2       2.2
    

    As you see, in the column 'electronic' should be in row '1' the value '1' and not '2.1'.

    Because of that, the hole DataSet would change. I hope someone could help me to find a solution for this.

提交回复
热议问题