baxx
baxx

Reputation: 4725

Reshape dataframe using pandas/numpy in a particular manner - convert multiple columns into two

Given the following data:

df = pd.DataFrame(
    dict(
        x1=["zero", "one", "two"],
        x2=["three", "four", "five"],
        x3=["six", "seven", "eight"],
        x4=["nine", "ten", "eleven"],
    )
)

which looks as:

In [2]: df
Out[2]:
     x1     x2     x3      x4
0  zero  three    six    nine
1   one   four  seven     ten
2   two   five  eight  eleven

I would like to reshape it to the following

x1      x2
zero    three
one     four
two     five
three   six
four    seven
five    eight
six     nine
seven   ten
eight   eleven

The following works, but I do not thing the approach is sound:

c1 = df.columns[: df.shape[1] - 1]
c2 = df.columns[1:]
d1 = df.loc[:, c1].T.values.flatten()
d2 = df.loc[:, c2].T.values.flatten()
pd.DataFrame(dict(x1=d1, x2=d2))

Upvotes: 1

Views: 47

Answers (1)

Chris Adams
Chris Adams

Reputation: 18647

Try np.vstack with iloc slicing in a list comprehension:

df_new = (pd.DataFrame(np.vstack([df.iloc[:,i:i+2].to_numpy()
                                   for i in range(df.shape[1]-1)]),
                      columns=['x1', 'x2']))

[out]

      x1      x2
0   zero   three
1    one    four
2    two    five
3  three     six
4   four   seven
5   five   eight
6    six    nine
7  seven     ten
8  eight  eleven

Upvotes: 1

Related Questions