Home > Net >  Pandas Groupby or Pivot to match values
Pandas Groupby or Pivot to match values

Time:03-05

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:

     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]})

CodePudding user response:

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
  • Related