Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas melt with column names and top row as column

Tags:

python

pandas

I have a dataframe df as where Col1, Col2 and Col3 are column names:

   Col1     Col2    Col3
           a        b
    B      2        3
    C     10        6

First row above with values a, b is subcategory so Col1 is empty for row 1.

I am trying to get the following:

B   Col2    a   2
B   Col3    b   3
C   Col2    a   10
C   Col3    b   6

I am not sure how to approach above.

Edit:

df.to_dict()
Out[16]: 
{'Unnamed: 0': {0: nan, 1: 'B', 2: 'C'},
 'Col2': {0: 'a', 1: '2', 2: '10'},
 'Col3': {0: 'b', 1: '3', 2: '6'}}
like image 914
Zanam Avatar asked Jan 25 '23 21:01

Zanam


2 Answers

Use stack and join

df_final = (df.iloc[1:].set_index('Col1').stack().reset_index(0)
                       .join(df.iloc[0,1:].rename('1')).sort_values('Col1'))

Out[345]:
     Col1   0  1
Col2    B   2  a
Col3    B   3  b
Col2    C  10  a
Col3    C   6  b
like image 122
Andy L. Avatar answered Jan 27 '23 09:01

Andy L.


You can try this replacing that NaN with a blank(or any string you want the colum to be named):

df.fillna('').set_index('Col1').T\
  .set_index('',append=True).stack().reset_index()

Output:

  level_0    Col1   0
0    Col2  a    B   2
1    Col2  a    C  10
2    Col3  b    B   3
3    Col3  b    C   6

df.fillna('Col0').set_index('Col1').T\
  .set_index('Col0',append=True).stack().reset_index(level=[1,2])

Output:

     Col0 Col1   0
Col2    a    B   2
Col2    a    C  10
Col3    b    B   3
Col3    b    C   6
like image 40
Scott Boston Avatar answered Jan 27 '23 10:01

Scott Boston