Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas Groupby or Pivot to match values

I struggle with a pandas dataframe where I want to match values by multiples in two columns.

My input Dataframe:

     X     Y      A       B       C
0   77  1001  705.0     NaN     NaN
1   77  1001    NaN    39.0     NaN
2  405   938    NaN  1188.0     NaN
3  587   167    NaN  1374.0     NaN
4  606   500    NaN   293.0     NaN
5  587   167    NaN     NaN   719.0
6  606   500    NaN     NaN  1775.0
7  405   938    NaN     NaN   705.0

Now I want to group where X and Y are dublicated. The result should be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     X     Y      A       B       C
0   77  1001  705.0    39.0     NaN
1  405   938    NaN  1188.0   705.0
2  587   167    NaN  1374.0   719.0
3  606   500    NaN   293.0  1775.0  

I’ve tried groudby and pivot but I dont get desired results. Thank You.

MWE:

df = pd.DataFrame({"X": [77, 77, 405, 587, 606, 587, 606, 405],
                   "Y": [1001, 1001, 938, 167, 500, 167, 500, 938],
                   "A": [705, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
                   "B": [np.nan, 39, 1188, 1374, 293, np.nan, np.nan, np.nan],
                   "C": [np.nan, np.nan, np.nan,np.nan,np.nan, 719, 1775, 705]})

>Solution :

Trygroupby with first

out = df.groupby(['X','Y'],as_index=False).first()
Out[550]: 
     X     Y      A       B       C
0   77  1001  705.0    39.0     NaN
1  405   938    NaN  1188.0   705.0
2  587   167    NaN  1374.0   719.0
3  606   500    NaN   293.0  1775.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading