Python 从数据框中获取每年的最后日期值

Python 从数据框中获取每年的最后日期值,python,pandas,dataframe,Python,Pandas,Dataframe,从1971年到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 3

从1971年到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
现在我想从数据框中获取每年的最后日期值。上面的例子我想要第40行。有人知道怎么做吗? 谢谢。

按年份使用:

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