python - 在 Pandas 的日期列中根据一年中的月份删除行

标签 python pandas datetime dataframe

我试图删除 12 月、1 月和 2 月的行。

注意:我设置的是日期作为索引。

df.drop(df.loc[(df.index.month==12) | (df.index.month==1) |   (df.index.month==2)])

最佳答案

你可以使用Series.isin:

# Boolean indexing.
# df = df.loc[~df.index.month.isin([12, 1, 2]), :] # For a copy.
df = df[~df.index.month.isin([12, 1, 2])]

# Equivalent code using df.drop.
df = df.drop(df.index[df.index.month.isin([12, 1, 2])])

关于python - 在 Pandas 的日期列中根据一年中的月份删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55437431/

相关文章:

python - Python 3.7 数据类中的类继承

python - Pandas asfreq 每周频率

sql-server - SSIS 中的日期和时间格式

swift - 使用 DateFormatter 解析深奥的日期字符串

sql - SQL Server 中的 LEFT 函数不返回结果

python - 单元测试 Flask 应用程序 View 和对话框

python - 用 Celery 取消已经执行的任务?

python - 使用 request.post 通过 python 发布多部分表单数据不起作用

python-2.7 - 使用pandas inplace关键字参数的准则

Python Pandas - 通过将标签与列匹配来将一个数据框的值添加到另一个数据框中