Home > other >  Find sum of each month Pandas
Find sum of each month Pandas

Time:05-11

df = pd.read_csv("wind_data.csv")
df = df[['SETTLEMENTDATE', 'wind']].copy()

dataset = df.set_index("SETTLEMENTDATE")
dataset.index = pd.to_datetime(dataset.index)
print(dataset.head())
print(dataset.shape)

Dataset

enter image description here

In this dataset I want to calculate wind data for each month. (I need only 12 rows of this data set instead 105350)

Can you please help me?

CodePudding user response:

Use DataFrame.resample:

dataset.resample('M')['wind'].sum()

CodePudding user response:

One way using a groupby:

df = pd.read_csv("wind_data.csv")
df = df[['SETTLEMENTDATE', 'wind']].copy()

dataset['SETTLEMENTMONTH'] = pd.to_datetime(dataset['SETTLEMENTDATE']).dt.floor('M')

dataset.groupby('SETTLEMENTMONTH')['wind'].sum()
  • Related