LearnerCode
LearnerCode

Reputation: 179

Older version of Panda, Groupby agg(list) does not work

Unfortunately for work the version of Python and its libraries are outdated. I am stuck on a groupby aggregation problem which I hope someone can help me with.

I have the following df

    Type            Cost
0   Food - Type A   1.50
1   Food - Type B   2.20
2   Car - Type A    1.30
3   Car - Type B    2.40
4   Car - Type C    3.70

I want create a category variable that I eventually want to group this df under.

category = df['Type'].str.split(' -', expand=True)[0]

0 Food
1 Food
2 Car
3 Car
4 Car

I then group this and turn each row into a list like this.

    dict= df.groupby(category).agg(list)
    
    0        Type                             Cost     
    Food    [Food - Type A, Food - Type B]             [1.5, 2.2]
    Car     [Car - Type A, Car - Type B, Car - Type C] [1,3, 2.4, 3.7]

Now this works normally. But the pandas I have to work with is version 0.16.2. And the agg(list) doesn't work. Perhaps it wasn't introduced yet, but I wasn't clear on the documentation.

Can someone advise how I may be able to recreate this?

Upvotes: 0

Views: 84

Answers (1)

Laurent
Laurent

Reputation: 13518

Here is another way to do it using pandas.pivot_table, which, it seems, was already possible with Pandas 0.16 according to the documentation:

import pandas as pd

df = pd.DataFrame(
    {
        "Type": [
            "Food - Type A",
            "Food - Type B",
            "Car - Type A",
            "Car - Type B",
            "Car - Type C",
        ],
        "Cost": [1.5, 2.2, 1.3, 2.4, 3.7],
    }
)
df["Category"] = df["Type"].str.split(" -", expand=True)[0]

df = df.pivot_table(values=["Type", "Cost"], index="Category", aggfunc=list)
                     Cost                                        Type
Category
Car       [1.3, 2.4, 3.7]  [Car - Type A, Car - Type B, Car - Type C]
Food           [1.5, 2.2]              [Food - Type A, Food - Type B]

Upvotes: 1

Related Questions