Reputation: 309
I have a Data frame like this(from 1971 to 2021).
date AUDUSD Close
42 2020-12-29 0.7608
41 2020-12-30 0.7676
40 2020-12-31 0.7709
39 2021-01-04 0.7664
38 2021-01-05 0.7767
37 2021-01-06 0.7799
36 2021-01-07 0.7767
now i want to get every year last date values from data frame. above example i want line No 40. Any one know how to do this? Thanks.
Upvotes: 1
Views: 52
Reputation: 862591
Use GroupBy.last
by years:
df['date'] = pd.to_datetime(df['date'])
df = df.groupby(df['date'].dt.year).last().reset_index(drop=True)
print (df)
date AUDUSD Close
0 2020-12-31 0.7709
1 2021-01-07 0.7767
Upvotes: 4