How to change data format with multiple columns by index?

Following here . The solution works for only one column. How to improve the solution for multiple columns. 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 ab
0 bc
1 cz
1 db

How to change them, for example

  0 1 2 3
0 abbc 
1 czdb

If df is

   0 1
0 ab
1 cz
1 db

Then

   0 1 2 3
0 ab NaN NaN
1 czdb

+4
source share
4 answers

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

In addition, 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
+3
source

Use the 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 an unequal number of indices - the constructor is called instead pd.DataFrame.

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
+2
source
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

, , .

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
+2

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)

Conclusion:

   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
+1
source

Source: https://habr.com/ru/post/1686537/


All Articles