conversion column names into first row

Viewed 5682

I would like to convert the following dataframe into a json .

df:
                               A   sector    B     sector    C     sector                              
TTM Ratio                      --   35.99   12.70  20.63  14.75      23.06
RRM Sales                      --  114.57    1.51   5.02   1.00    4594.13
MQR book                     1.48    2.64    1.02   2.46   2.73       2.74
TTR cash                       --   14.33    7.41  15.35   8.59  513854.86

In order to do so by using the function df.to_json() I would need to have unique names in column and indices.

Therefore what I am looking for is to convert the column names into a row and have default column numbers . In short I would like the following output:

df:
                               0     1       2       3       4       5 
                               A   sector    B     sector    C     sector                              
TTM Ratio                      --   35.99   12.70  20.63  14.75      23.06
RRM Sales                      --  114.57    1.51   5.02   1.00    4594.13
MQR book                     1.48    2.64    1.02   2.46   2.73       2.74
TTR cash                       --   14.33    7.41  15.35   8.59  513854.86

Turning the column names into the first row so I can make the conversion correctly .

2 Answers

You could also use vstack in numpy:

>>> df
   x  y  z
0  8  7  6
1  6  5  4

>>> pd.DataFrame(np.vstack([df.columns, df]))
   0  1  2
0  x  y  z
1  8  7  6
2  6  5  4

The columns become the actual first row in this case.

Related