我有一個 df 我想過濾多年 Pandas
資料
id type stat date
aa ss y 2022-01-01
bb tt y 2023-01-05
cc uu n 2023-01-05
aa hi y 2021-01-01
aa hi n 2021-02-01
期望的
id type stat date
aa ss y 2022-01-01
bb tt y 2023-01-05
cc uu n 2023-01-05
我希望檢索 2022 年和 2023 年。
正在做
df = df[df['date'].dt.year == 2022 & 2023]
任何建議表示贊賞
uj5u.com熱心網友回復:
強制日期時間并按 dt.year 過濾
df[pd.to_datetime(df['date']).dt.year.isin([2022,2023])]
uj5u.com熱心網友回復:
你也可以檢查 between
df[df.date.between('2022-01-01','2024-01-01')]
id type stat date
0 aa ss y 2022-01-01
1 bb tt y 2023-01-05
2 cc uu n 2023-01-05
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/420308.html
標籤:
上一篇:如何使用numpy創建一組亂數
