You can use the groupby
method in pandas to group the rows by a common key (like a category or date) and then aggregate the values using a function like sum
, mean
, or a custom function. Here is an example code to aggregate 12 rows into one row by grouping them by a common key:
main.py293 chars13 lines
Output:
main.py54 chars4 lines
In this example, we created a dataframe with 12 rows of data, where each row represents a value for a specific date. We then used the groupby
method to group the rows by the date column and used the agg
method to aggregate the values of each group using the sum
function. This resulted in a new dataframe with one row representing the total sum of values for the date '2021-01-01'.
gistlibby LogSnag