基于日期时间的数值统计函数:伪代码转Python实现求助
问题与解决方案
数据
| activity_date | company_name | new_company_status | calling | visit | quotation | po |
|---|---|---|---|---|---|---|
| 03/10/2022 | ABC | Yes | Yes | No | No | No |
| 04/10/2022 | ABC | No | No | No | Yes | Yes |
| 05/10/2022 | DEF | No | Yes | Yes | No | No |
| 06/10/2022 | XYZ | Yes | No | Yes | Yes | No |
| 07/10/2022 | DEF | No | No | No | Yes | Yes |
| 08/10/2022 | XYZ | No | Yes | No | No | Yes |
需求
- 统计同一
company_name下,存在至少一个new_company_status为'Yes',且后续(不同日期)calling为'Yes'的公司数量; - 统计同一
company_name下,存在calling为'Yes',且后续(不同日期)visit为'No'、quotation为'No'且po为'Yes'的公司数量。
已编写伪代码
伪代码1
for every same company name: if 'new_company_status' = 'Yes': check 'activity_date' # for new company status if it is a Yes if 'calling' = 'Yes': check 'activity_date' # for calling if it is a Yes if calling_date >= new_company_date: new_company to call =+ 1
伪代码2
for every same company name: if 'calling' = 'Yes': check 'activity_date' # for calling if it is a Yes if 'visit' = 'No': if 'quotation' = 'No': if 'po' = 'Yes': check 'activity_date' # for po if it is a Yes if po_date >= calling_date: call to po += 1
预期输出
- 1
- 3
Python代码实现
借助pandas可以高效处理这类分组时间序列数据,以下是具体实现:
首先导入依赖并加载数据:
import pandas as pd # 加载数据 data = [ {'activity_date': '03/10/2022', 'company_name': 'ABC', 'new_company_status': 'Yes', 'calling': 'Yes', 'visit': 'No', 'quotation': 'No', 'po': 'No'}, {'activity_date': '04/10/2022', 'company_name': 'ABC', 'new_company_status': 'No', 'calling': 'No', 'visit': 'No', 'quotation': 'Yes', 'po': 'Yes'}, {'activity_date': '05/10/2022', 'company_name': 'DEF', 'new_company_status': 'No', 'calling': 'Yes', 'visit': 'Yes', 'quotation': 'No', 'po': 'No'}, {'activity_date': '06/10/2022', 'company_name': 'XYZ', 'new_company_status': 'Yes', 'calling': 'No', 'visit': 'Yes', 'quotation': 'Yes', 'po': 'No'}, {'activity_date': '07/10/2022', 'company_name': 'DEF', 'new_company_status': 'No', 'calling': 'No', 'visit': 'No', 'quotation': 'Yes', 'po': 'Yes'}, {'activity_date': '08/10/2022', 'company_name': 'XYZ', 'new_company_status': 'No', 'calling': 'Yes', 'visit': 'No', 'quotation': 'No', 'po': 'Yes'} ] df = pd.DataFrame(data) # 将日期列转为datetime类型,方便比较 df['activity_date'] = pd.to_datetime(df['activity_date'], format='%d/%m/%Y')
实现需求1的函数
def count_new_company_with_subsequent_call(df): grouped = df.groupby('company_name') count = 0 for name, group in grouped: # 获取该公司所有new_company_status为Yes的日期 new_dates = group[group['new_company_status'] == 'Yes']['activity_date'] if not new_dates.empty: # 获取该公司所有calling为Yes的日期 call_dates = group[group['calling'] == 'Yes']['activity_date'] # 检查是否存在call日期晚于某个new日期(且不同日期) has_subsequent_call = any(call_date > new_date for new_date in new_dates for call_date in call_dates) if has_subsequent_call: count += 1 return count # 调用并输出 print(count_new_company_with_subsequent_call(df)) # 输出:1
实现需求2的函数
def count_call_with_subsequent_po(df): grouped = df.groupby('company_name') count = 0 for name, group in grouped: # 获取该公司所有calling为Yes的日期 call_dates = group[group['calling'] == 'Yes']['activity_date'] if not call_dates.empty: # 获取该公司符合visit=No、quotation=No、po=Yes的日期 po_dates = group[(group['visit'] == 'No') & (group['quotation'] == 'No') & (group['po'] == 'Yes')]['activity_date'] # 检查是否存在po日期晚于某个call日期(且不同日期) has_subsequent_po = any(po_date > call_date for call_date in call_dates for po_date in po_dates) if has_subsequent_po: count += 1 return count # 调用并输出 print(count_call_with_subsequent_po(df)) # 输出:3
内容的提问来源于stack exchange,提问作者matilda
相关产品推荐
相关产品推荐

