My question is related with the selection of the first most frequent element for each group of a DataFrameGroupBy object. Specifically, I am having the following dataframe:
data = [
[1000, 1, 1], [1000, 1, 1], [1000, 1, 1], [1000, 1, 2], [1000, 1, 2],
[1000, 1, 2], [2000, 0, 1], [2000, 0, 1], [2000, 1, 2],
[2000, 0, 2], [2000, 1, 2]]
df = pd.DataFrame(data, columns=['route_id', 'direction_id', 'trip_id'])
Then, I group my df based on the columns route_id, direction_id by using:
t_groups = df.groupby(['route_id','direction_id'])
I would like to store the value of the trip_id column based on the first most popular trip_id of each unique route_id, direction_id combination.
Ι have tried to apply a function value_counts() but I cannot get the first popular trip_id value.
I would like my expected output to be like:
route_id direction_id trip_id
0 1000 1 1
1 2000 0 1
2 2000 1 2
Any suggestions?