将某些像元值相加时是否有其他解决方案

问题描述

我将python3与pandas包和SequenceMatcher结合使用,以输出A.OUT-B.INA.OUT-C.IN的值。程序计算总是显示ValueError: Must have equal len keys and value when setting with an iterable的最后两行的值时。我认为这是因为代码求和函数的一部分具有两个值,而不是一个。例如,当我print(df.loc[(df.StopName.isin(["B"])) & (df.MO.str.contains(df.loc[index-1,"MO"])),"AmountOfInput"].sum())时。它将显示B个停止输入的总数,它们具有相同(几乎90%相似)的MO,值是31526,另一个是倒数第二个值7074。这样就不能进行subs操作并弹出错误。我搜索Google并阅读了熊猫文档,仍然找不到解决方法。

这里是数据框

import pandas as pd
from difflib import SequenceMatcher
df = pd.DataFrame({'MO': ['510-20200701001','510-20200701001','510-20200701002','510-20200701003','510-20200701004','510-20200701005','510-20200701006','510-20200701006_02','510-20200701006_02_01','510-20200701006_02_01'],'StopName': ['A','B','C','A','C'],'AmountOfInput': [21000,22112,22476,12000,12609,12775,15000,15595,15844,600,775,790,1000,1149,1176,6000,6225,6289,32180,24452,24859,7074,7271],'AmountOfOutput': [22400,22057,22330,12800,12586,12685,16000,15587,15718,800,783,1200,1139,1162,6400,6278,24437,24532,6958,7108],'A.OUT-B.IN':['','',''],'A.OUT-C.IN': ['','Match':[True,True,False,False]})

这是过滤器代码

for index,row in df.iterrows():
    if index not in df.loc[df['Match'].isin([False]),"MO"].index:
        if index in df.loc[(df.StopName.isin(["B"])) & (df["A.OUT-B.IN"].values == "")].index:
            df.loc[index,"A.OUT-B.IN"] = df.loc[index-1,"AmountOfOutput"] - df.loc[index,"AmountOfInput"]
        elif index in df.loc[(df.StopName.isin(["C"])) & (df["A.OUT-C.IN"].values == "")].index:
            df.loc[index,"A.OUT-C.IN"] = df.loc[index-2,"AmountOfInput"]         
    else:
        ratio = SequenceMatcher(None,df.loc[index-1,"MO"],df.loc[index,"MO"]).ratio()
        if ratio >= 0.9:
            if "B" in df[df.MO.str.contains(df.loc[index,"MO"])]["StopName"].values:
                df.loc[(df.StopName.isin(["B"])) & (df.MO.isin([df.loc[index-1,"MO"]])),"A.OUT-B.IN"] = df.loc[(df.StopName.isin(["A"])) & (df.MO.isin([df.loc[index-1,"AmountOfOutput"].values - df.loc[(df.StopName.isin(["B"])) & (df.MO.str.contains(df.loc[index-1,"AmountOfInput"].sum()
                df.loc[(df.StopName.isin(["B"])) & (df.MO.isin([df.loc[index,"A.OUT-B.IN"] = df.loc[(df.StopName.isin(["B"])) & (df.MO.isin([df.loc[index-1,"A.OUT-B.IN"].values
            elif "C" in df[df.MO.str.contains(df.loc[index,"MO"])]["StopName"].values:
                df.loc[(df.StopName.isin(["C"])) & (df.MO.isin([df.loc[index-1,"A.OUT-C.IN"] = df.loc[(df.StopName.isin(["A"])) & (df.MO.isin([df.loc[index-1,"AmountOfOutput"].values - df.loc[(df.StopName.isin(["C"])) & (df.MO.str.contains(df.loc[index-1,"AmountOfInput"].sum()
                df.loc[(df.StopName.isin(["C"])) & (df.MO.isin([df.loc[index,"A.OUT-C.IN"] = df.loc[(df.StopName.isin(["C"])) & (df.MO.isin([df.loc[index-1,"A.OUT-C.IN"].values

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...