pandas -Python,根据“日期"列删除行 [英] Pandas - Python, deleting rows based on Date column

查看:208
本文介绍了 pandas -Python,根据“日期"列删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于一个日期列删除数据框的行; [Delivery Date]

I'm trying to delete rows of a dataframe based on one date column; [Delivery Date]

我需要删除大于6个月但不等于'1970'的行.

I need to delete rows which are older than 6 months old but not equal to the year '1970'.

我创建了2个变量:

from datetime import date, timedelta
sixmonthago = date.today() - timedelta(188)

import time
nineteen_seventy = time.strptime('01-01-70', '%d-%m-%y')

但是我不知道如何使用[Delivery Date]列基于这两个变量删除行.

but I don't know how to delete rows based on these two variables, using the [Delivery Date] column.

谁能提供正确的解决方案?

Could anyone provide the correct solution?

推荐答案

您可以将它们过滤掉:

df[(df['Delivery Date'].dt.year == 1970) | (df['Delivery Date'] >= sixmonthago)]

这将返回年份为1970或日期小于6个月的所有行.

This returns all rows where the year is 1970 or the date is less than 6 months.

您可以使用布尔索引并传递多个条件来过滤df,对于多个条件,您需要使用数组运算符,因此要使用|而不是or,并由于运算符的优先级而在条件周围加上括号.

You can use boolean indexing and pass multiple conditions to filter the df, for multiple conditions you need to use the array operators so | instead of or, and parentheses around the conditions due to operator precedence.

检查文档以获取布尔索引的说明

这篇关于 pandas -Python,根据“日期"列删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆