import pandas as pd pd.set_option("display.max_rows", 5)
This function lets you define a new column in your data, which is a single number calculated either across all the data, or within specified groups. It will result in a DataFrame with as many rows as the number of unique groups, or if no groups are defined, one row.
from siuba import _, group_by, summarize from siuba.data import mtcars
Summarize over everything¶
When you use summarize with an ungrouped DataFrame, the result is a single row.
mtcars >> summarize(avg_mpg = _.mpg.mean())
Summarizing per group¶
When you use summarize with a grouped DataFrame, the result has the same number of rows as there are groups in the data. For example, there are 3 values of cylinders (
cyl) a row can have (4, 6, or 8), so the result will be 3 rows.
(mtcars >> group_by(_.cyl) >> summarize(avg_mpg = _.mpg.mean()) )
Note that summarize also accepts a single value, like a string or number.
(mtcars >> group_by(_.cyl) >> summarize( measure = "mean miles per gallon", value = _.mpg.mean() ) )
|0||4||mean miles per gallon||26.663636|
|1||6||mean miles per gallon||19.742857|
|2||8||mean miles per gallon||15.100000|
Edit page on github here. Interactive version: