I have a data frame and I would like to group it by a particular column (or, in other words, by values from a particular column). I can do it in the following way: gro
I would use a custom aggregator as shown below.
d = pd.DataFrame([[1,"man"], [1, "woman"], [1, "girl"], [2,"man"], [2, "woman"]],columns = 'number family'.split())
d
Here is the output:
number family
0 1 man
1 1 woman
2 1 girl
3 2 man
4 2 woman
Now the Aggregation taking first and last elements.
d.groupby(by = "number").agg(firstFamily= ('family', lambda x: list(x)[0]), lastFamily =('family', lambda x: list(x)[-1]))
The output of this aggregation is shown below.
firstFamily lastFamily
number
1 man girl
2 man woman
I hope this helps.