Merge or concat df’s with uneven rows – python

You can use itertools.zip_longest to align the groups:

from itertools import zip_longest

g1 = df1.groupby('ValueX')
g2 = df2.groupby('ValueY')
g3 = df3.groupby('ValueZ')

dfs = []
for (_, a), (_, b), (_, c) in zip_longest(g1, g2, g3, fillvalue=('', pd.DataFrame())):
    dfs.append( 
        pd.concat([a.reset_index(drop=True), 
                   b.reset_index(drop=True), 
                   c.reset_index(drop=True)], axis=1) )

final = pd.concat(dfs).reset_index(drop=True)
print(final)

Prints:

    ValueX Item3 Item4  ValueY Item1 Item2  ValueZ Item5 Item6
0      0.0     a     f     1.0     a     a     6.0     a     f
1      0.0     e     s     1.0     c     e     6.0     e     s
2      0.0     d     h     1.0     c     d     6.0     d     h
3      0.0     g     k     1.0     g     g     6.0     g     k
4      0.0     f     f     1.0     a     f     6.0     f     f
5      0.0     c     d     NaN   NaN   NaN     NaN   NaN   NaN
6      2.0     c     c     3.0     c     c     7.0     c     d
7      2.0     f     g     3.0     c     c     7.0     c     c
8      2.0     b     b     3.0     g     f     7.0     f     g
9      2.0     c     k     3.0     a     b     7.0     b     b
10     2.0     j     j     3.0     c     c     7.0     c     k
11     NaN   NaN   NaN     3.0     c     j     7.0     j     j
12     4.0     g     n     5.0     g     g     8.0     g     n
13     4.0     m     r     5.0     a     m     8.0     m     r
14     4.0     s     d     5.0     c     s     8.0     s     d
15     4.0     c     x     5.0     c     c     8.0     c     x
16     4.0     k     d     5.0     g     k     8.0     k     d
17     NaN   NaN   NaN     5.0     k     k     NaN   NaN   NaN

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top