Skip to content
Advertisement

Selecting first row from each subgroup (pandas)

How to select the subset of rows where distance is lowest, grouping by date and p columns?

JavaScript

Ideally, the returned dataframe should contain:

JavaScript

Advertisement

Answer

One way is to use groupby + idxmin to get the index of the smallest distance per group, then use loc to get the desired output:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement