I am making a machine learning model on the titanic dataset. Click
Because I can't just pass in "mode" in the aggfunc
parameter like I can pass "mean", I made a lambda function to do the job. But because of it "< lambda >" shows up in the pivot table. How can I change it to display something like "mode"?
I can definitely create a function by the name mode
but is there some other way?
CodePudding user response:
I'm actually not sure, but by reading the documentation I think that since you can use a lambda function, you could also create a function and set a name for it.
CodePudding user response:
I get your desired behavior by using the class method directly:
import pandas
import seaborn
df = seaborn.load_dataset("titanic")
pandas.pivot_table(
data=df,
values=["pclass", "age", "sibsp"],
index="survived",
aggfunc=[
"median",
pandas.Series.mode # <-- no lambda
]
)
The result:
median mode
age pclass sibsp age pclass sibsp
survived
0 28.0 3 0 21.0 3 0
1 28.0 2 0 24.0 1 0
This implies that you can define any function and it's __name__
attribute will be used.