AttributeError DataFrameGroupBy object has no attribute cost
This error occurs when you try to access an attribute or method that does not exist for a pandas DataFrameGroupBy object.
For example, if you have grouped a DataFrame by a column and are trying to access the 'cost' column of the resulting DataFrameGroupBy object, you will get this error because the DataFrameGroupBy object does not have a 'cost' attribute.
To access a particular column of the grouped DataFrame, you can use the apply() method with a lambda function that returns the desired column:
grouped_df = df.groupby('column_name')
cost_column = grouped_df.apply(lambda x: x['cost'])
This will return a new DataFrame with the 'cost' column for each group
原文地址: https://www.cveoy.top/t/topic/fELj 著作权归作者所有。请勿转载和采集!