0

I have an issue that i have been trying to figure out for a while, i have a data frame which contains a column with column headers, and a column with the data, i needs to transform it from long to wide with the column headers as the data from the column_names column.

my data is in the format as below

column_names    data
0   col1    a
1   col2    b
2   col3    c
3   col1    d
4   col2    e
5   col3    f
6   col1    h
7   col2    i
8   col3    j 

and i want it to be in the format of:

col1    col2    col3
0   a   b   c
1   d   e   f
2   h   i   j

I have tried using pivot and transpose, neither of which seem to do the job.

any help would be much appreciated!

code for making sample dfs: current data:

d = {'column_names': ['col1','col2','col3','col1','col2','col3','col1','col2','col3'],
    'data' : ['a','b','c','d','e','f','h','i','j']}

df=pd.DataFrame(data=d)
df

Desired output:

x = {'col1':['a','d','h'],
    'col2': ['b','e','i'],
    'col3': ['c','f','j']}

df2=pd.DataFrame(data=x)
df2
am_93
  • 1

0 Answers0