user8602973
user8602973

Reputation: 31

Find sum of each month Pandas

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?

Upvotes: 0

Views: 620

Answers (2)

Learning is a mess
Learning is a mess

Reputation: 8277

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()

Upvotes: 0

ansev
ansev

Reputation: 30940

Use DataFrame.resample:

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

Upvotes: 2

Related Questions