Skip to content Skip to sidebar Skip to footer

Python / Pandas: How To Merge Rows In Dataframe

After merging of two data frames: output = pd.merge(df1, df2, on='ID', how='outer') I have data frame like this: index x y z 0 2 NaN 3 0 NaN 3 3 1 2 N

Solution 1:

Perhaps, you could take mean on them.

In [418]: output.groupby('index', as_index=False).mean()
Out[418]:
   index    x    y  z
0      0  2.0  3.0  3
1      1  2.0  3.0  4

Solution 2:

We can group the DataFrame by the 'index' and then... we can just get the first values with .first() or minimum with .min() etc. depending on the case of course. What do you want to get if the values in z differ?

In [28]: gr = df.groupby('index', as_index=False)

In [29]: gr.first()
Out[29]:
   index    x    y  z
0      0  2.0  3.0  3
1      1  2.0  3.0  4

In [30]: gr.max()
Out[30]:
   index    x    y  z
0      0  2.0  3.0  3
1      1  2.0  3.0  4

In [31]: gr.min()
Out[31]:
   index    x    y  z
0      0  2.0  3.0  3
1      1  2.0  3.0  4

In [32]: gr.mean()
Out[32]:
   index    x    y  z
0      0  2.0  3.0  3
1      1  2.0  3.0  4

Post a Comment for "Python / Pandas: How To Merge Rows In Dataframe"