2

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?

Shital
  • 53
  • 6
  • Does this answer your question? [How can I pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe) – Ynjxsjmh Aug 10 '22 at 11:19

2 Answers2

1

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)
Cristian Ispan
  • 571
  • 2
  • 5
  • 23
1

You may provide more details like the index and values of the expected output, the operation you wanna do, etc, so that we could give a specific solution to your case

Here is the solution:

import pandas as pd
import io
import numpy as np

data_string = """    columns_name
0   a
1   b
2   c
3   d
4   e
"""
df = pd.read_csv(io.StringIO(data_string), sep='\s+')

# Solution
df_result = pd.DataFrame(data=[[np.nan]*5],
                         columns=df['columns_name'].tolist())