I have the following dataset (df
). I want to groupby
it using brand as my index, get the mean of workers and value columns and the first count of provider column.
brand workers value provider
H&M 322 56 mark
H&M 450 433 mark
Lindex 678 233 luke
Lindex 543 456 luke
Levi 234 32 chris
Levi 789 12 chris
Now I can
df = df.groupby('brand')['workers', 'value', 'provider'].agg({'workers': mean, 'value':mean, 'provider' : first).reset_index()
but consider that my real dataset as way more columns I want to take the mean and I don't want to specify each of them, is there a better way of declaring a default function?
Sort of "take the mean of all the non string columns and the first observation of the string columns?"
In pandas, the groupby function can be combined with one or more aggregation functions to quickly and easily summarize data. This concept is deceptively simple and most new pandas users will understand this concept.
Aggregate using one or more operations over the specified axis. Function to use for aggregating the data. If a function, must either work when passed a DataFrame or when passed to DataFrame.
Instead of using groupby aggregation together, we can perform groupby without aggregation which is applicable to aggregate data separately.
Pandas DataFrame agg() Method The agg() method allows you to apply a function or a list of function names to be executed along one of the axis of the DataFrame, default 0, which is the index (row) axis. Note: the agg() method is an alias of the aggregate() method.
No, but it isn't that hard to write some code to do it for you.
f = dict.fromkeys(df, 'mean')
f.update(
dict.fromkeys(df.columns[df.dtypes.eq(object)], 'first'))
print(f)
{'brand': 'first', 'provider': 'first', 'value': 'mean', 'workers': 'mean'}
You then pass f
to agg
.
df = df.groupby('brand')['workers', 'value', 'provider'].agg(f)
If you want to reset the index, you will have to remove the grouper from f
.
del f['brand']
df = df.groupby('brand', as_index=False)['workers', 'value', 'provider'].agg(f)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With