提取特定年份的所有行值



给定以下 Pandas 数据帧,如何仅提取 2019 年的数据,即 0.5 和 0.2?如果这还不够,我很抱歉。我是熊猫的新手。我认为我拥有的对象是Pandas.series对象。

我正在使用python 3。

我正在获取我的数据:

数据 = get_data((

此数据来自外部库

Date
2018-09-05    0.2
2019-05-10    0.5
2019-09-05    0.2
2020-05-12    0.5
2020-06-08    0.5

使用DatetimeIndex.year获取年份 # 并进行比较:

df[pd.to_datetime(df.index).year == 2019]
2019-05-10    0.5
2019-09-05    0.2
Name: Date, dtype: float64

或者,作为列表,

df[pd.to_datetime(df.index).year == 2019].tolist()
# [0.5, 0.2]

最新更新