Skip to content
Advertisement

perform df.loc to groupby df

I’ve a df consisted of person, origin and destination

JavaScript

the df:

JavaScript

I have grouped by the df with df_grouped = df.groupby(['O','D']) and match them with another dataframe, taxi.

JavaScript

similarly, I group by the taxi with their O and D. Then I merged them after aggregating and counting the PersonID and TaxiID per O-D pair. I did it to see how many taxis are available for how many people.

JavaScript

Now, I want to perform df.loc to take only those PersonID that was counted in the merged file. How can I do this? I’ve tried to us:

JavaScript

but it returns an empty dataframe. What can I do to do this?

edit: I attach the complete code for this case using dummy data

JavaScript

Advertisement

Answer

Select MultiIndex by tuple with Series.explode for scalars from nested lists:

JavaScript

For better performance is possible use set comprehension with flatten:

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