Skip to content Skip to sidebar Skip to footer

Reshape A Pandas Dataframe With Multiple Columns

I have an issue in reshaping a pandas DatFrame. It looks like this (the numbers of lines and columns can vary) : columns col1 col2 col3 col4 Species

Solution 1:

First create MultiIndex by floor division and then reshape by stack and unstack:

c = np.array(['c','f','p'])
df.index = [df.index, c[np.arange(len(df.index)) % 3]]
print (df)
columns          col1        col2        col3        col4
Species                                                  
sp1     c  218.000000  521.000000  533.000000  793.000000
        f    0.105569    0.252300    0.258111    0.384019
        p    2.000000    2.000000    2.000000    3.000000
sp2     c  225.000000  521.000000  540.000000  800.000000
        f    0.107862    0.249760    0.258869    0.383509
        p    2.000000    2.000000    2.000000    3.000000
sp3     c  217.000000  477.000000  512.000000  725.000000
        f    0.112377    0.247022    0.265148    0.375453
        p    1.000000    1.000000    3.000000    3.000000

df = df.stack().unstack(1).reset_index()
print (df)
   Species columns      c         f    p
0      sp1    col1  218.0  0.105569  2.0
1      sp1    col2  521.0  0.252300  2.0
2      sp1    col3  533.0  0.258111  2.0
3      sp1    col4  793.0  0.384019  3.0
4      sp2    col1  225.0  0.107862  2.0
5      sp2    col2  521.0  0.249760  2.0
6      sp2    col3  540.0  0.258869  2.0
7      sp2    col4  800.0  0.383509  3.0
8      sp3    col1  217.0  0.112377  1.0
9      sp3    col2  477.0  0.247022  1.0
10     sp3    col3  512.0  0.265148  3.0
11     sp3    col4  725.0  0.375453  3.0

Post a Comment for "Reshape A Pandas Dataframe With Multiple Columns"