简体   繁体   English

想要在 python 中出现某个任务名称的第一个日期

[英]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 IDTask 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.

相关问题 我想在 python 中打印名字姓氏和姓氏名字 - I want to print first name last and second name first in python Python-取与列中第一个日期的时差 - Python - take the time difference from the first date in a column 将两个数据集加载到 python 中,并希望从第二个数据集中获取一列并放入第一个数据集中 - Loaded two datasets into python and want to take a column from second dataset and put into first dataset 过滤并取出日期的某些元素 - Filter and take out certain elements of a date 每当用户在我的音乐机器人中发送消息时,它只需要第一个词。 我想让它取整首歌的名字 - Whenever a user sends a message in my music bot, it only takes the first word. I want it to take the name of the full song 在ndarray python中获取第一个维度 - Take first dimension in ndarray python python 在不同文件夹中查找相同文件名,并利用两个文件完成某项任务 - Find same file name in different folders, and utilize two files for a certain task in python 如何将 csv 行的第一项作为 python 中的列表名称? - how can I take csv row's first item as a list name in python? 通过python采取特定的日期间隔 - Take a specific date interval by python Python:如何获取多个.csv文件的列的第一个值及其名称,并使用它们创建一个新文件 - Python: How to take the first value of a column of multiple .csv files + its name and make a new file with them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM