1

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   NaN  4
  1   NaN   3   4
...

How to merge rows with the same index? Expected output:

index  x   y  z
  0    2   3  3
  1    2   3  4
6
  • What happens if the values in z differ? Does that ever happen? Commented Jul 18, 2017 at 9:52
  • You're merging on 'ID', but it isn't anywhere in your dataframe. Feels like we're missing some data. Commented Jul 18, 2017 at 9:53
  • This is an example, in the code I have this column Commented Jul 18, 2017 at 9:54
  • @IanS When vaules in 'z' are different, it is still happen Commented Jul 18, 2017 at 9:56
  • 2
    This is a very simple question (in terms of reproducibility), if you want the best answers, always post a reproducible question. Include original dataframes as well. Commented Jul 18, 2017 at 10:03

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.