how to aggregate 12 rows values to create one row pandas in python

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.py
import pandas as pd

# create sample data
data = {'date': ['2021-01-01'] * 12,
        'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}

df = pd.DataFrame(data)

# group by date and aggregate the values using sum
aggregated_df = df.groupby('date').agg({'value': 'sum'})

print(aggregated_df)
293 chars
13 lines

Output:

main.py
            value
date             
2021-01-01     78
54 chars
4 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'.

related categories

gistlibby LogSnag