Bharath M Shetty
Bharath M Shetty

Reputation: 30605

How to reshape a multi-column dataframe by index?

Following from here . The solution works for only one column. How to improve the solution for multiple columns. i.e If I have a dataframe like

df= pd.DataFrame([['a','b'],['b','c'],['c','z'],['d','b']],index=[0,0,1,1])
   0  1
0  a  b
0  b  c
1  c  z
1  d  b

How to reshape them like

  0   1  2  3
0  a  b  b  c 
1  c  z  d  b

If df is

   0  1
0  a  b
1  c  z
1  d  b

Then

   0  1   2  3
0  a  b NaN  NaN
1  c  z   d  b

Upvotes: 3

Views: 633

Answers (4)

Scott Boston
Scott Boston

Reputation: 153460

Let's try

df1 = df.set_index(df.groupby(level=0).cumcount(), append=True).unstack()
df1.set_axis(labels=pd.np.arange(len(df1.columns)), axis=1)

Output:

   0  1  2  3
0  a  b  b  c
1  c  d  z  b

Output for df with NaN:

   0     1  2     3
0  a  None  b  None
1  c     d  z     b

Upvotes: 1

piRSquared
piRSquared

Reputation: 294278

pd.DataFrame({n: g.values.ravel() for n, g in df.groupby(level=0)}).T

   0  1  2  3
0  a  b  b  c
1  c  z  d  b

This is all over the place and I'm too tired to make it pretty

v = df.values
cc = df.groupby(level=0).cumcount().values
i0, r = pd.factorize(df.index.values)
n, m = v.shape
j0 = np.tile(np.arange(m), n)
j = np.arange(r.size * m).reshape(-1, m)[cc].ravel()
i = i0.repeat(m)

e = np.empty((r.size, m * r.size), dtype=object)

e[i, j] = v.ravel()

pd.DataFrame(e, r)

   0  1     2     3
0  a  b  None  None
1  c  z     d     b

Upvotes: 2

Zero
Zero

Reputation: 76917

Use flatten/ravel

In [4401]: df.groupby(level=0).apply(lambda x: pd.Series(x.values.flatten()))
Out[4401]:
   0  1  2  3
0  a  b  b  c
1  c  z  d  b

Or, stack

In [4413]: df.groupby(level=0).apply(lambda x: pd.Series(x.stack().values))
Out[4413]:
   0  1  2  3
0  a  b  b  c
1  c  z  d  b

Also, with unequal indices

In [4435]: df.groupby(level=0).apply(lambda x: x.values.ravel()).apply(pd.Series)
Out[4435]:
   0  1    2    3
0  a  b  NaN  NaN
1  c  z    d    b

Upvotes: 3

cs95
cs95

Reputation: 402523

Use groupby + pd.Series + np.reshape:

df.groupby(level=0).apply(lambda x: pd.Series(x.values.reshape(-1, )))

   0  1  2  3
0  a  b  b  c
1  c  z  d  b

Solution for unequal number of indices - call the pd.DataFrame constructor instead.

df

   0  1
0  a  b
1  c  z
1  d  b

df.groupby(level=0).apply(lambda x: \
      pd.DataFrame(x.values.reshape(1, -1))).reset_index(drop=True)

   0  1    2    3
0  a  b  NaN  NaN
1  c  z    d    b

Upvotes: 2

Related Questions