Python Grouping (groupby)

Asked 2 years ago, Updated 2 years ago, 42 views

Attempt to retrieve the maximum/minimum value of the dataframe using the groupping function. I don't think the code is wrong, but I wonder why the error occurs.

class_group=df.groupby('Pclass') # Grouping of Class 1,2,3 variables
class_group.max()

The class_group.min() result also displays the same error.

python pandas

2022-09-20 18:02

1 Answers

df.groupby("Pclass") creates groups grouped by Pclass.

Here, you usually name the column and apply the collective function to it.

df.groupby("Pclass")["Age"].max() This will result in the largest age in each group.

Since the questioner's code did not specify the column name to perform max or min, it will perform max and min for all columns. However, depending on the type of column, max, min cannot be obtained because there are also columns (you can't tell the size of a regular string), so I think an error occurred for those columns.


2022-09-20 18:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.