python - 如何进行数据帧减法?
问题描述
我有以下数据框: Col1 是付款。
Col1 Value
Item1 100
Item2 200
Item3 300
Col2 是项目成本
Col2 Value
Project1 200
Project2 300
Project3 400
我基本上想将 Col1 与 Col2 中的项目相匹配。
如何获得以下数据框?
Col2 Value Col1
Project1 100 Item1
Project1 100 Item2
Project2 100 Item2
Project2 200 Item3
Project3 100 Item3
Project3 300
我可以在熊猫中做些什么来获得这个结果吗?非常感谢
解决方案
在这里,您可以找到逻辑,添加注释以进行解释:
# making list of project requirement and item value to iterate
item_cost = list(zip(payment['Col1'], payment['value']))
requirment = list(zip(project_cost['Col2'], project_cost['value']))
d = {}
for project, cost in requirment:
item_used = []
try:
while cost > 0:
if item_cost[0][1] <= cost: # if item is having less cost than requirement
cost -= item_cost[0][1] # making requirement less by item cost
item_used.append((item_cost[0][1], item_cost[0][0]))
item_cost = item_cost[1:] # removing item from item_cost once used
else:
item_cost[0] = ((item_cost[0][0], item_cost[0][1] - cost))
item_used.append((cost, item_cost[0][0]))
cost = 0
except: # when item list will be finished exception will be called
item_used.append((cost,np.nan))
d[project] = item_used
# d is dict having project as key and item used
project_cost['Col1'] = project_cost['Col2'].map(d)
pr_cost = project_cost.explode("Col1")
pr_cost['value'] = pr_cost['Col1'].apply(lambda x:x[0])
pr_cost['Col1'] = pr_cost['Col1'].apply(lambda x:x[1])
根据您的新数据,我们只需将列名更改为上述代码:
item_cost = list(zip(payment['Payment'], payment['Value']))
requirment = list(zip(project_cost['Project'], project_cost['Cost']))
d = {}
for project, cost in requirment:
item_used = []
try:
while cost > 0:
if item_cost[0][1] <= cost: # if item is having less cost than requirement
cost -= item_cost[0][1] # making requirement less by item cost
item_used.append((item_cost[0][1], item_cost[0][0]))
item_cost = item_cost[1:] # removing item from item_cost once used
else:
item_cost[0] = ((item_cost[0][0], item_cost[0][1] - cost))
item_used.append((cost, item_cost[0][0]))
cost = 0
except: # when item list will be finished exception will be called
item_used.append((cost,np.nan))
d[project] = item_used
project_cost['bank_val'] = project_cost['Project'].map(d)
pr_cost = project_cost.explode("bank_val")
pr_cost['value'] = pr_cost['bank_val'].apply(lambda x:x[0])
pr_cost['bank'] = pr_cost['bank_val'].apply(lambda x:x[1])
print(pr_cost.drop(columns = ['bank_val', 'Cost']))
推荐阅读
- reactjs - 声明变量时使用箭头函数而不是赋值有什么好处?
- bash - 重击;如何从文件中的一行文本中检索“_foo”之后的所有内容?
- javascript - 如何使用在javascript中选中的复选框将表行附加到一个表到另一个表
- laravel - 自定义刀片指令中的缓存问题 - Laravel
- pepper - Pepper Development 兼容性和版本
- javascript - 按文本选择链接(完全匹配)并替换 href
- vue.js - Vue Vuetify:无效的道具:道具“值”的自定义验证器检查失败。发现在--->
- javascript - 如何在 mapbox GL JS 上按日期范围过滤数据?
- python - 如何将嵌套字典的值分配给python中的单独变量?
- python - 编写递归函数来计算编号。图表