le Minh Nguyen
le Minh Nguyen

Reputation: 281

include groupby column values in result

I use groupby by "department" and then apply a function to the column "flight_length" The desired result look something like this

department co2_footprint
Account Managment 10974
Customer Service 562
HHRR 2321

However, only the co2_footprint column was returned (the value column). I want to include the department column as well. How do I do that?

Here is the code that I used:

def co2_footprint(value):
    return value * 0.1

print(df.groupby('department')['flight_length'].apply(co2_footprint))

what the data look like: enter image description here

Upvotes: 0

Views: 50

Answers (2)

Corralien
Corralien

Reputation: 120391

Your function is not correct, missing an aggregate function like sum, mean or whatever aggregation custom function:

Try:

def co2_footprint(value):
    return sum(value * 0.1)
>>> df.groupby('department')['flight_length'].apply(co2_footprint).reset_index()

  department  flight_length
0  Marketing     159.914594

Upvotes: 1

Yashar Ahmadov
Yashar Ahmadov

Reputation: 1636

Try this df.groupby('department')['flight_length'].apply(co2_footprint).reset_index()

If it does not work, better way is to use pd.pivot_table() function. Read more here:

https://pandas.pydata.org/docs/reference/api/pandas.pivot_table.html

Upvotes: 0

Related Questions