Sorting groupby result by order of appearance of 1st occurence in group keys #2209
-
Hi, Let's consider this example: import pandas as pd
import vaex
import numpy as np
from functools import partial
pdf = pd.DataFrame({'group_keys':[1,4,2,4,1], 'val':range(5)})
vdf = vaex.from_pandas(pdf)
def sort_key(val, key_order):
return np.where(key_order == val)[0][0]
sort_key2 = partial(sort_key, key_order = np.unique(pdf["group_keys"]))
vdf = vaex.from_pandas(pdf)
agg_res = vdf.groupby(by="group_keys", sort=False).agg({"val":"first"}) agg_res
Out[34]:
# group_keys val
0 1 0
1 2 2
2 4 1 Please, how to re-order pdf.groupby("group_keys", sort=False).agg(**{"val":("val","first")}).reset_index()
Out[36]:
group_keys val
0 1 0
1 4 1
2 2 2 Thanks for your help! |
Beta Was this translation helpful? Give feedback.
Answered by
JovanVeljanoski
Sep 14, 2022
Replies: 1 comment 1 reply
-
Why don't you do the groupby aggregation in whatever way and sort the results of your |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
yohplala
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why don't you do the groupby aggregation in whatever way and sort the results of your
val
column afterwards? Or am I missing something?