Is there any posibilities to round the mean of C in this group by?
df.groupby('A').agg({'B':'sum', 'C':'mean'})
agg() is used to pass a function or list of function to be applied on a series or even each element of series separately. In case of list of function, multiple results are returned by agg() method.
agg is an alias for aggregate . Use the alias. A passed user-defined-function will be passed a Series for evaluation. The aggregation is for each column.
agg is an alias for aggregate . Use the alias. Functions that mutate the passed object can produce unexpected behavior or errors and are not supported. See Mutating with User Defined Function (UDF) methods for more details.
groupby() function is used to split the data into groups based on some criteria. pandas objects can be split on any of their axes. The abstract definition of grouping is to provide a mapping of labels to group names. sort : Sort group keys.
You can place round after the aggregation as shown below:
df.groupby('A').agg({'B':'sum', 'C':'mean'}).round(2)
In an aggregation it is not possible to include round inside. Hence you can place round after the aggregation. It is a simple and effective way.
Round(2) will round it off to two decimal places. You can change 2 to include whatever number of decimal places you want to round off the numbers to. For eg to round off to 3 decimal places you can use .round(3) and so on.
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