Python Dataframes: How to merge every two rows of a Dataframe together?

Viewed 121

I have this Dataframe with 500 rows:

Shape       Number of Tags                                        Tags
   P              5                         [k1, k3k, k561, k76, k911]
   P              5                      [k2w1, k452, k6d1, k82, k9n1]
   P              5                        [k0g1, k112, k3g, k5x, k8q]
   P              5                      [k361, k692, kbb1, kc41, kf9]
  ...            ...                                               ...
   P              5                         [hv, k1w, k242, kd62, khr]
   P              5                          [2x1, k6w, kf, kmf, kv32]
   P              5                       [k471, kd4, kkk1, km62, xf1]
   P              5                         [k031, k3j1, k4q, kqx, pc]

I want it to be like this with 250 rows: Basically merge every two rows together.

Shape    Number of Tags                                                                      Tags
 PP             10                        [k1, k3k, k561, k76, k911, k2w1, k452, k6d1, k82, k9n1]
 PP             10                       [k0g1, k112, k3g, k5x, k8q, k361, k692, kbb1, kc41, kf9]
 ...            ...                                                                           ...
 PP             10                            [hv, k1w, k242, kd62, khr, 2x1, k6w, kf, kmf, kv32]
 PP             10                         [k471, kd4, kkk1, km62, xf1, k031, k3j1, k4q, kqx, pc]
1 Answers

In your case get the divisor and groupby

 out = df.groupby(df.index//2).apply(lambda x : x.sum())
Related