I use groupby by "department" and then apply a function to the column "flight_length" The desired result look something like this
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))
CodePudding user response:
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
CodePudding user response:
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