[英]Want to take first date appearing for a certain task name in python
I have an excel table that reads:我有一个 excel 表,上面写着:
Task Name任务名称 | End Date结束日期 | Process ID进程 ID |
---|---|---|
TASK 1任务1 | 1st feb 2月1日 | 65461 65461 |
TASK 2任务 2 | 2nd feb 2月2日 | 65461 65461 |
TASK 1任务1 | 3rd feb 2 月 3 日 | 65461 65461 |
and i have a code that matches the ID and takes in the date for task 1 but I want to take the first end date for that task in python.我有一个与 ID 匹配的代码,并输入任务 1 的日期,但我想在 python 中获取该任务的第一个结束日期。 Below is my code: '''下面是我的代码:'''
import pandas as pd
import numpy as np
df1 = pd.read_excel("Purchase Requisition and Purchase Order Approval Workflow Tasks.xlsx")
p_id,tk,ed =df1['Process ID'].tolist(),df1['Task Name'].tolist(),df1['End Date'].tolist()
ed = pd.to_datetime(df1['End Date'])
a = len(p_id)
x=np.array(p_id)
x=np.unique(p_id)
b=len(x)
inrfqdate=[None]*b
for i in range(0,b):
for j in range(0,a):
if x[i] == p_id[j]:
if ("Initiate Purchase Requisition" in tk[j]):
inrfqdate[i] = ed[j]
''' This takes the end date for latest task name appearing. ''' 这将采用最新任务名称出现的结束日期。 I jut want it to store the first task date.我只是希望它存储第一个任务日期。
Here's a DataFrame I created to test my solution.这是我为测试我的解决方案而创建的 DataFrame。
import pandas as pd
# Test Data Frame
data = {'Task Name': ['TASK 1', 'TASK 2', 'TASK 1', 'TASK 1', 'TASK 2', 'TASK 2'],
'End Date': ['2/1/2021', '2/2/2021', '2/3/2021', '2/7/2021', '2/8/2021',
'2/10/2021'],
'Process ID': ['64561', '64561', '64561', '64562', '64562', '64562'],
}
df = pd.DataFrame(data)
That gave me the following table:这给了我下表:
Then I changed the dtype in the End Date
column from an Object to a DateTime and added an additional column to find the Min Date
grouped by Process ID
and Task Name
然后我将End Date
列中的 dtype 从 Object 更改为 DateTime 并添加了一个附加列以查找按Process ID
和Task Name
分组的Min Date
# Convert Date Column from Object to DateTime
df['End Date'] = pd.to_datetime(df['End Date'])
# Create a 'Min Date' column and groupby parameters to select minimum date
df['Min Date'] = df.groupby(['Process ID', 'Task Name'])['End Date'].transform('min')
That gave me the following output:这给了我以下 output:
You could also replace the current data in End Date
by using:您还可以使用以下方法替换End Date
中的当前数据:
df['End Date'] = df.groupby(['Process ID', 'Task Name'])['End Date'].transform('min')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.