【问题标题】:How to iterate through rows in one dataframe and assgin value如何遍历一个数据框中的行并赋值
【发布时间】:2020-03-16 01:09:30
【问题描述】:

我有一个名为资源的数据框

资源 Xy 使用已于 06-12-2018 11:00 结束 .直到P1的Per1使用Xy资源,P2的Per8不能使用该资源。我想将项目 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 row: row['new']+row['Time'] if row['new'] != pd.to_datetime('1970-01-01 00:00:00') else pd.to_datetime('1970-01-01 00:00:00'),axis = 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 Per 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

【问题讨论】:

  • 每个人分配的时间是多少?
  • 情况不同。它将在数小时内给予。我已经添加了专栏。请再过一遍
  • 所以你没有说一个有所有分配时间的表?如果是这样,复制并粘贴它会很有帮助
  • 我无法复制粘贴表格。请通过docs.google.com/spreadsheets/d/…

标签: python pandas loops dataframe iteration


【解决方案1】:

首先我们在 pandas 中获取数据并将列转换为合适的类型:

import pandas as pd
from io import StringIO

txt = """
Project Resource    Person  Allocation Time start   end
P1  Xy  Per 1   4:00    06-12-2018 8:00 06-12-2018 11:00
P1  Z   Per 2   5:00    06-12-2018 8:00 06-12-2018 12:00
P1  Y   Per 3   7:00    06-12-2018 8:00 06-12-2018 18:00
P1  X   Per 1   3:50    06-12-2018 8:00 06-12-2018 12:00
P2  X   Per 6   2:20    01-01-1970 0:00 01-01-1970 0:00
P2  Y   Per 7   1:25    01-01-1970 0:00 01-01-1970 0:00
P2  Xy  Per 8   2:30    01-01-1970 0:00 01-01-1970 0:00
P2  Xy  Per 9   14:00   01-01-1970 0:00 01-01-1970 0:00
P2  X   Per 7   12:35   01-01-1970 0:00 01-01-1970 0:00
P2  Y   Per 6   11:10   01-01-1970 0:00 01-01-1970 0:00
P2  Z   Per 11  13:45   01-01-1970 0:00 01-01-1970 0:00
P2  Z   Per 12  10:00   01-01-1970 0:00 01-01-1970 0:00
P3  X   Per 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.assign(
    start=lambda f: pd.to_datetime(f.start),
    end=lambda f: pd.to_datetime(f.end),
    alloc_time=lambda f: pd.to_timedelta(f["Allocation Time"].apply(lambda x: x+':00')))

我们遍历资源组,每次都使用前一个结束作为新的开始:

out = t_m.copy()
for resource, group in t_m.groupby('Resource'):
    start = group.start.iloc[0]
    for ix, row in group.iterrows():
        out.loc[ix, 'start'] = start
        start = start + row['alloc_time']
        out.loc[ix, 'end'] = start

out = out.drop('Allocation Time', axis=1)
print(out)
   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

【讨论】:

    猜你喜欢
    • 2019-12-15
    • 1970-01-01
    • 2021-10-01
    • 2018-05-17
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2018-06-26
    相关资源
    最近更新 更多