1

I have a df with rows appended to next to other row.

I want columns of the df to be alpha,beta,gamma

                   0           1           2           3           4         5


        0        alpha        beta         gamma       alpha        beta      gamma


        1          a           b            c           1             2         3

Resultant df should be,

                                    alpha        beta         gamma   

                            0         a           b            c         


                            1         1           2             3

I want my df's first rows to be the index of dataframe?

What i have tried:

I tried to make the first row as index.

            new_header = df.iloc[0] #grab the first row for the header
            df = df[1:] #take the data less the header row
            df.columns = new_header

1 Answers1

0

You can try pivot:

(df.T.assign(col=df.iloc[0].eq('alpha').cumsum())
   .pivot(index='col', columns=0, values=1)
   .rename_axis(index=None, columns=None)
)

Output:

  alpha beta gamma
1     a    b     c
2     1    2     3
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74