Skip to content
Advertisement

How to convert first column of dataframe in to its headers

I have dataframe df:

    0
0   a
1   b
2   c
3   d
4   e

O/P should be:

   a  b  c  d  e
0
1
2
3
4
5

I want column containing(a, b,c,d,e) as header of my dataframe.

Could anyone help?

Advertisement

Answer

If your dataframe is pandas and its name is df. Try solving it with pandas:

Firstly convert initial df content to a list, afterwards create a new dataframe defining its columns with the list.

import pandas as pd

list = df[0].tolist()    #df[0] is getting the content of first column
dfSolved = pd.DataFrame([], columns = list)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement