Reputation: 41
My dataframe look like this:
mth account_type interest_rate
1057 1977-01-01 Special 6.5
1061 1977-02-01 Special 6.5
1065 1977-03-01 Special 6.5
1069 1977-04-01 Special 6.5
1073 1977-05-01 Special 6.5
... ... ... ...
3077 2019-02-01 Special 5
3081 2019-03-01 Special 5
3085 2019-04-01 Special 5
3089 2019-05-01 Special 5
3093 2019-06-01 Special 5
I like to collapse "mth" column to just year
mth account_type interest_rate
1057 1977 Special 6.5
... ... ... ...
3093 2019 Special 5
Any help would be very much appreciated. Many Thanks!
Upvotes: 0
Views: 27
Reputation: 619
First you have to add a column with the year:
df['year'] = df['mth'].dt.year
And then you can group the info by year and do for example an .mean()
df.groupby((["interest_rate"]).mean()
Upvotes: 0
Reputation: 4618
If your column mth
is already datetime
:
df['mth'] = df['mth'].dt.year
If it is a string
you have to first convert to datetime
:
df['mth'] = pd.to_datetime(df['mth'])
Upvotes: 1