python - 根据条件分配值

标签 python python-3.x pandas dataframe

我有一个数据库可以跟踪一家建筑公司的服务和安装收入。我正在尝试跟踪技术人员的失误,其中技术人员为销售人员和销售收入设定了领先地位。在此示例中,从数据库中提取,Justin 是创建销售线索的技术人员;肖恩是去接电话的推销员,在这种情况下,他卖掉了电话;最后,维克多是安装这项工作的人。所有推销员的总计为 0,因为公司在安装完成后才收款。如果 project_id == 0 则意味着没有根据原始调用创建潜在客户。一个项目编号至少可以跨越 2 行,最多可以跨越 3 行。

例子:

         project_id    emp_name     client_name     ...    invoice_date    total
...         ...          ...            ...         ...        ...          ...
15315     26173042      Justin         Bill W       ...     2021-03-26     169.95
...         ...          ...            ...         ...        ...          ...
15322     26173042      Sean           Bill W       ...     2021-03-27       0
...         ...          ...            ...         ...        ...          ...
15347     26173042      Victor         Bill W       ...     2021-04-01     17235
15348        0          Justin         Jane D       ...     2021-04-01      285
...         ...          ...            ...         ...        ...          ...

我想感谢 Justin 设置电话,促成 17.2k 美元的销售额,我想感谢 Sean 出售这份工作。我更愿意创建一个名为 sales_total 的新列来捕获销售数据。

期望的输出:

         project_id    emp_name     client_name     ...    invoice_date    total    sales_total
...         ...          ...            ...         ...        ...          ...         ...
15315     26173042      Justin         Bill W       ...     2021-03-26     169.95      17235
...         ...          ...            ...         ...        ...          ...         ...
15322     26173042      Sean           Bill W       ...     2021-03-27       0         17235
...         ...          ...            ...         ...        ...          ...         ...
15347     26173042      Victor         Bill W       ...     2021-04-01     17235       17235
15348        0          Justin         Jane D       ...     2021-04-01      285          0
...         ...          ...            ...         ...        ...          ...         ... 

我尝试过使用pd.groupbypd.sort_valuespd.iloc;但没有成功。我不太清楚如何为所需的行分配正确的值。如果有人知道解决方案或可以指出正确的方向,我们将不胜感激。

最佳答案

如果您有此数据框(假设记录已排序 = 安装作业在最后):

   index  project_id emp_name client_name invoice_date     total
0  15315    26173042   Justin      Bill_W   2021-03-26    169.95
1  15322    26173042     Sean      Bill_W   2021-03-27      0.00
2  15347    26173042   Victor      Bill_W   2021-04-01  17235.00
3  15348           0   Justin      Jane_D   2021-04-01    285.00

然后:

df["sales_total"] = df.groupby("project_id")["total"].transform("last")
print(df)

创建 sales_total 列:

   index  project_id emp_name client_name invoice_date     total  sales_total
0  15315    26173042   Justin      Bill_W   2021-03-26    169.95      17235.0
1  15322    26173042     Sean      Bill_W   2021-03-27      0.00      17235.0
2  15347    26173042   Victor      Bill_W   2021-04-01  17235.00      17235.0
3  15348           0   Justin      Jane_D   2021-04-01    285.00        285.0

关于python - 根据条件分配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66959718/

相关文章:

python - 拟合函数返回 TypeError : float() argument must be string or a number in ScikitLearn

python - 将嵌套列表分成具有不相交元素的组

python - 从字符串中删除 "."和 "\"

python-3.x - pandas 数据帧中的跳转点 : the moment when the value in a column gets changed

python - 仅在数据框中计算几列的 LOG10

python - 值错误 : need more than 1 value to unpack

python - 使用谷歌应用程序引擎部署 Bottle 应用程序时出现问题

Python3 和编码 : different on linux and on OSX?

python-3.x - 运行 ibapi 的 EReader 线程中的异常错误

python - 在 panda 中使用多个 groupby 计数绘制条形图