'Merge two lists in pandas groupby and apply
I have a dataframe such as:
make model year range
0 Audi A3 [1991, 1992, 1993]
1 Audi A3 [1997, 1998]
I need:
make model year range
0 Audi A3 [1991, 1992, 1993, 1997, 1998]
I have tried
df = df['year range'].groupby([df.make, df.model]).apply(list).reset_index()
However I end up with the year range being a list of lists instead of a single list.
It seems simple enough but I can't figure it out!
Solution 1:[1]
Concatenating lists is done by addition, so you can simply apply sum to the relevant column:
In [24]: df
Out[24]:
make model year
0 Audi A3 [1991, 1992, 1993]
1 Audi A3 [1997, 1998]
In [25]: df.groupby([df.make, df.model]).year.apply(sum)
Out[25]:
make model
Audi A3 [1991, 1992, 1993, 1997, 1998]
Name: year, dtype: object
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fuglede |
