May-11-2017, 06:10 PM
Selecting rows with row number=1 over partition can be emulated with
To get dataframe containing all "first_ones" rows and all rows with df.CrCode == 9000 without eventual duplicities
.groupby()
followed by .first()
:df['absdatediff'] = abs(df.DateDiff) first_ones = df.reset_index().sort_values('absdatediff').groupby('ID_x').first().set_index('index')reset_index() and set_index is used to preserve original index for joining with rows with df.CrCode_y == 9000
To get dataframe containing all "first_ones" rows and all rows with df.CrCode == 9000 without eventual duplicities
pd.Index.union()
can be used:df.loc[pd.Index.union(df[df.DrCode_y == 9000].index, first_ones.index)]