user3702643
user3702643

Reputation: 1495

Extract all row values for a specific year

Given the following Pandas dataframe, how to I extract only the data from 2019, which is 0.5 and 0.2? I am sorry if this is not enough information. I am new to pandas. I think the object I have is a Pandas.series object.

I am using python 3.

I am getting my data as such:

data = get_data()

This data comes from an external library

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

Upvotes: 2

Views: 2023

Answers (1)

cs95
cs95

Reputation: 402333

Use DatetimeIndex.year to get the year # and compare:

df[pd.to_datetime(df.index).year == 2019]

2019-05-10    0.5
2019-09-05    0.2
Name: Date, dtype: float64

Or, as a list,

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

Upvotes: 1

Related Questions