Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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 编写一个if条件,使用date来过滤记录,以获取数据过滤器_Python_Pandas - Fatal编程技术网

Python 编写一个if条件,使用date来过滤记录,以获取数据过滤器

Python 编写一个if条件,使用date来过滤记录,以获取数据过滤器,python,pandas,Python,Pandas,我有过去几年用户登录我的系统的数据。我想对过去三个月内从我的系统中退出的用户进行分析 我有三个条件 A (All) = All Accepted(Activated) users as of August 31st N (new) = Accepted users in last three months (Jun, July, Aug) R (returning) = Accepted before June, but logged in last three months i.e. (

我有过去几年用户登录我的系统的数据。我想对过去三个月内从我的系统中退出的用户进行分析

我有三个条件

A (All) = All Accepted(Activated) users as of August 31st 
N (new) = Accepted users in last three months (Jun, July, Aug)
R  (returning) = Accepted before June, but logged in last three months i.e.  (Jun, July, Aug)
D = Dropping 
下降=A-N-R

数据如下所示-

我想将数据过滤到N和R类别中,并存储到csv中,得到D的值

我写了这个逻辑

df = pd.read_csv("work_hrs.csv")

from datetime import datetime
import pdb
new_col = []
threshold_act_date = datetime.strptime("2019-6-01", '%Y-%m-%d').date()
threshold_log_date = datetime.strptime("2019-8-21", '%Y-%m-%d').date()

for row in df.iloc[:,[2,3]].values:
    try:
        last_log = datetime.strptime(row[0][:10], '%Y-%m-%d').date()
        active_in = datetime.strptime(row[1][:10], '%Y-%m-%d').date()

        if last_log >= threshold_log_date:
            if active_in >= threshold_act_date:
                new_col.append("N")
            else:
                new_col.append("R")
        else:
            new_col.append("D")
    except:
        new_col.append("not_active")

total_came = len(df) - Counter(df["status"])["unregistered"] - Counter(df["status"])["notregister"] - Counter(df["status"])["nan"]

print("Total come to our platform so far :", total_came)

dropout = total_came - Counter(df["new_stat"])["N"] - Counter(df["new_stat"])["R"]

print("The total no. of dropouts are :",dropout)
上述代码是否正确地将日期过滤到N和R类别中

预期产出:

1. D which will give count of dropouts from the system in last three months
2. CSV file which contains All new (N) users in last 3 months 
3. CSV file which contains all returning (R) users in last three months