Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除日期为'的行;基于键值的所需日期点之前的_Python_Python 3.x_Pandas_Datetime_Dataframe - Fatal编程技术网

Python 删除日期为'的行;基于键值的所需日期点之前的

Python 删除日期为'的行;基于键值的所需日期点之前的,python,python-3.x,pandas,datetime,dataframe,Python,Python 3.x,Pandas,Datetime,Dataframe,我有一个pd.dataframe,看起来像这样: key_value date value_01 2017-01-13 value_01 2018-02-17 value_01 2018-04-02 value_01 2018-05-13 value_01 2018-05-16 value_02 2017-01-18 value_02 2018-03-13 value_02 2018-04-01 value_02 2018-05-16 valu

我有一个pd.dataframe,看起来像这样:

key_value     date
value_01   2017-01-13
value_01   2018-02-17
value_01   2018-04-02
value_01   2018-05-13
value_01   2018-05-16  
value_02   2017-01-18
value_02   2018-03-13
value_02   2018-04-01
value_02   2018-05-16  
value_02   2018-05-22  
value_03   2018-01-13
value_03   2018-04-14
 key_value     date
value_01   2018-04-02
value_01   2018-05-13
value_01   2018-05-16  
value_02   2018-04-01
value_02   2018-05-16  
value_02   2018-05-22  
value_03   2018-04-14
现在根据
键的值

我想删除日期列值早于2018-04-01的所有行

我希望有这样的最终输出:

key_value     date
value_01   2017-01-13
value_01   2018-02-17
value_01   2018-04-02
value_01   2018-05-13
value_01   2018-05-16  
value_02   2017-01-18
value_02   2018-03-13
value_02   2018-04-01
value_02   2018-05-16  
value_02   2018-05-22  
value_03   2018-01-13
value_03   2018-04-14
 key_value     date
value_01   2018-04-02
value_01   2018-05-13
value_01   2018-05-16  
value_02   2018-04-01
value_02   2018-05-16  
value_02   2018-05-22  
value_03   2018-04-14

您可以使用布尔索引筛选数据帧。这里没有分组操作。只需记住首先将序列转换为
datetime

df['date'] = pd.to_datetime(df['date'])

res = df[~(df['date'] < '2018-04-01')]

print(res)

   key_value       date
2   value_01 2018-04-02
3   value_01 2018-05-13
4   value_01 2018-05-16
7   value_02 2018-04-01
8   value_02 2018-05-16
9   value_02 2018-05-22
11  value_03 2018-04-14
df['date']=pd.to_datetime(df['date'])
res=df[~(df['date']<'2018-04-01')]
打印(res)
关键值日期
2价值_01 2018-04-02
3价值_01 2018-05-13
4价值_01 2018-05-16
7价值_02 2018-04-01
8价值_02 2018-05-16
9价值_02 2018-05-22
11价值_03 2018-04-14

也许这段代码不是最好的,但即使您的日期没有排序,它也会按照您的要求执行

import pandas as pd
from datetime import datetime

d = {'key_value': [1, 2, 3, 4, 5], 'date': ['2017-01-13', '2018-02-17','2018-04-02','2018-05-13','2018-05-16']}#create dataframe

date_string='2018-04-01'#date limit
date_to_drop=datetime.strptime(date_string, '%Y-%m-%d')# conmert my date to datetime
i=0
l=len(d['date'])#len of your set of date
while i<l:#loop on your set of date
    datetime_object = datetime.strptime(d['date'][i], '%Y-%m-%d')#convert the current date in datetime
    if datetime_object<date_to_drop:#if my current date is previous of the date limit I delete it from my dataframe
        d['date'].pop(i)#delete the date
        d['key_value'].pop(i)#delete the key_value
        l-=1#decrese the len of the date set of 1 seeing that I delete an element
    else:#if my current date is after of date limit I just pass to next iteration
        i+=1
df = pd.DataFrame(data=d)
print (df)

有点晚了,但这是我的解决方案。试着在没有熊猫的情况下使用一些蟒蛇类的东西。也许更容易阅读

从日期时间导入日期时间
数据={}
specificDate=datetime.strTime(“2018-04-01”,“Y-%m-%d”)
数据更新({“value_01”:[“2017-01-13”、“2018-02-17”、“2018-04-02”、“2018-05-13”、“2018-05-16”]})
数据更新({“value_02”:[“2017-01-18”、“2018-03-13”、“2018-04-01”、“2018-05-16”、“2018-05-22”]))
数据更新({“value_03”:[“2018-01-13”,“2018-04-14”]})
对于数据中的键。键():
data.update({key:list(filter(lambda x:datetime.strtime(x,“%Y-%m-%d”)>=specificDate,data[key]))
对于键,data.items()中的值:
打印(键)
对于val值:
打印(“+val”)
输出:

value_01
    2018-04-02
    2018-05-13
    2018-05-16
value_02
    2018-04-01
    2018-05-16
    2018-05-22
value_03
    2018-04-14