-2

I have a df, with a column for labels and another for all its samples:

    network k
0       c1  25.0
1       c1  30.0
3       z_7 11.0
4       z_7 13.0

How do I transpose it, turning each label into a column name, with its samples as column values, like so:

    c1    z_7
0   25.0  11.0
1   30.0  13.0
8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
  • 2
    Does this answer your question? https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe – Paul H Apr 21 '22 at 00:26

1 Answers1

0
df = pd.pivot(df, columns='network', values='k')
c1 = df['c1'].dropna().reset_index(drop=True)
z_7 = df['z_7'].dropna().reset_index(drop=True)
print(pd.concat([c1, z_7], axis=1))

Output:

     c1   z_7
0  25.0  11.0
1  30.0  13.0
BeRT2me
  • 12,699
  • 2
  • 13
  • 31
  • There's probably a better way to do this, but this worked at least – BeRT2me Apr 21 '22 at 01:22
  • Actually not in my case because my real data will generate loads of Nans due to differences in column lengths, so your nan treatment unlikely served my purpose better than all those infinite answers – 8-Bit Borges Apr 21 '22 at 01:24