Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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 如何在一个dataframe和assgin值中遍历行_Python_Pandas_Loops_Dataframe_Iteration - Fatal编程技术网

Python 如何在一个dataframe和assgin值中遍历行

Python 如何在一个dataframe和assgin值中遍历行,python,pandas,loops,dataframe,iteration,Python,Pandas,Loops,Dataframe,Iteration,我有一个叫做资源的数据帧 资源Xy使用于2018年12月6日11:00结束 。在Per1为P1使用资源Xy之前,Per8为P2不能使用资源。我想将项目P1中使用的结束值资源Xy复制到Per8使用的项目P2的开始列,替换默认值。结束列将是(开始时间+为该人员分配的时间)。我想一次又一次地迭代这些行,直到替换所有默认值(1970-01-01 00:00:00) t_mx=t_m.groupby(['Resource']).agg({'end':max}).reset_index() t_mx['

我有一个叫做资源的数据帧

资源Xy使用于2018年12月6日11:00结束 。在Per1为P1使用资源Xy之前,Per8为P2不能使用资源。我想将项目P1中使用的结束值资源Xy复制到Per8使用的项目P2的开始列,替换默认值。结束列将是(开始时间+为该人员分配的时间)。我想一次又一次地迭代这些行,直到替换所有默认值(1970-01-01 00:00:00)


t_mx=t_m.groupby(['Resource']).agg({'end':max}).reset_index()
t_mx['end']=t_m.apply(lambda行:行['new']+行['Time'],如果行['new']!=pd.to_日期时间('1970-01-01 00:00:00'),否则pd.to_日期时间('1970-01-01 00:00:00'),轴=1)
我已经使用上面的代码创建了一个列

我不知道如何进一步进行

项目资源人员分配时间开始结束 P1 Xy每1 04:00 06-12-2018 08:00 06-12-2018 11:00 P1 Z每2 05:00 06-12-2018 08:00 06-12-2018 12:00 P1 Y每3 07:00 06-12-2018 08:00 06-12-2018 18:00 P1 X每1 03:50 06-12-2018 08:00 06-12-2018 12:00 P2 X每6 02:20 01-01-1970 00:00 01-01-1970 00:00 P2 Y根据7 01:25 01-01-1970 00:00 01-01-1970 00:00 P2 Xy根据8 02:30 01-01-1970 00:00 01-01-1970 00:00 P2 Xy每9 14:00 01-01-1970 00:00 01-01-1970 00:00 P2 X每7 12:35 01-01-1970 00:00 01-01-1970 00:00 P2 Y根据6 11:10 01-01-1970 00:00 01-01-1970 00:00 P2 Z每11 13:45 01-01-1970 00:00 01-01-1970 00:00 P2 Z每12 10:00 01-01-1970 00:00 01-01-1970 00:00
P3 X每5 07:30 01-01-1970 00:00 01-01-1970 00:00

首先,我们获取熊猫数据,并将熊猫柱转换为合适的类型:

将熊猫作为pd导入
从io导入StringIO
txt=”“”
项目资源人员分配时间开始结束
P1 Xy每1 4:00 06-12-2018 8:00 06-12-2018 11:00
P1 Z每2 5:00 06-12-2018 8:00 06-12-2018 12:00
P1 Y每3 7:00 06-12-2018 8:00 06-12-2018 18:00
P1 X每1 3:50 06-12-2018 8:00 06-12-2018 12:00
P2 X每6 2:20 01-01-1970 0:00 01-01-1970 0:00
P2 Y根据7 1:25 01-01-1970 0:00 01-01-1970 0:00
P2 Xy每8 2:30 01-01-1970 0:00 01-01-1970 0:00
P2 Xy根据9 14:00 01-01-1970 0:00 01-01-1970 0:00
P2 X每7 12:35 01-01-1970 0:00 01-01-1970 0:00
P2 Y根据6 11:10 01-01-1970 0:00 01-01-1970 0:00
P2 Z每11 13:45 01-01-1970 0:00 01-01-1970 0:00
P2 Z每12 10:00 01-01-1970 0:00 01-01-1970 0:00
P3 X每5 7:30 01-01-1970 0:00 01-01-1970 0:00
"""
t_m=pd.read_csv(StringIO(txt),sep='\t')
t_m=t_m分配(
开始=lambda f:pd.to_datetime(f.start),
end=λf:pd.至_日期时间(f.end),
alloc_time=lambda f:pd.to_timedelta(f[“分配时间”]。应用(lambda x:x+':00'))
我们迭代资源组,每次使用前一个端点作为新的起点:

out=t\m.copy()
对于资源,在t_m.groupby(“资源”)中分组:
start=group.start.iloc[0]
对于ix,在group.iterrows()中的行:
out.loc[ix,‘start']=start
开始=开始+行['alloc_time']
out.loc[ix,‘end']=开始
out=out.drop('分配时间',轴=1)
打印(输出)

分配给每个人的时间是多少?各不相同。它将在几个小时内发出。我已经添加了这个专栏。请再检查一遍,这样你就不必说一张表上有所有分配的时间了?如果soI在复制粘贴表格时遇到问题,那么复制并粘贴该表格会很有帮助。请通过
   Project Resource  Person               start                 end alloc_time
0       P1       Xy   Per 1 2018-06-12 08:00:00 2018-06-12 12:00:00   04:00:00
1       P1        Z   Per 2 2018-06-12 08:00:00 2018-06-12 13:00:00   05:00:00
2       P1        Y   Per 3 2018-06-12 08:00:00 2018-06-12 15:00:00   07:00:00
3       P1        X   Per 1 2018-06-12 08:00:00 2018-06-12 11:50:00   03:50:00
4       P2        X   Per 6 2018-06-12 11:50:00 2018-06-12 14:10:00   02:20:00
5       P2        Y   Per 7 2018-06-12 15:00:00 2018-06-12 16:25:00   01:25:00
6       P2       Xy   Per 8 2018-06-12 12:00:00 2018-06-12 14:30:00   02:30:00
7       P2       Xy   Per 9 2018-06-12 14:30:00 2018-06-13 04:30:00   14:00:00
8       P2        X   Per 7 2018-06-12 14:10:00 2018-06-13 02:45:00   12:35:00
9       P2        Y   Per 6 2018-06-12 16:25:00 2018-06-13 03:35:00   11:10:00
10      P2        Z  Per 11 2018-06-12 13:00:00 2018-06-13 02:45:00   13:45:00
11      P2        Z  Per 12 2018-06-13 02:45:00 2018-06-13 12:45:00   10:00:00
12      P3        X   Per 5 2018-06-13 02:45:00 2018-06-13 10:15:00   07:30:00