I have a dataset, df1
, that looks as follows:
Date | ID | City | State | Value1 | Value2 | Value3 |
---|---|---|---|---|---|---|
2020-03 | 10001 | Los Angeles | CA | 10 | 22 | 346 |
2020-03 | 10002 | Sacramento | CA | 321 | 4440 | 221 |
... | .... | ... | ... | ... | ... | ... |
2022-04 | 10001 | Los Angeles | CA | 133 | 1001 | 2330 |
2022-04 | 10002 | Sacramento | CA | 2343 | 110 | 1230 |
I want to delete all values after 2021-07. What code can I use to do so? That way, I want my range to be from 2020-03
to 2021-07
.
Note: my date
type is period [M]
CodePudding user response:
You can just do
out = df.loc[df.Date.between('2020-03','2021-07')]
CodePudding user response:
You can use string indexing and slice like this:
df = pd.DataFrame({'A':np.arange(36)},
index = pd.period_range('2020-01', '2022-12', freq='M'))
df.loc['2020-06':'2021-01']
Output:
A
2020-06 5
2020-07 6
2020-08 7
2020-09 8
2020-10 9
2020-11 10
2020-12 11
2021-01 12