【发布时间】:2020-09-04 06:58:03
【问题描述】:
大家,
我正在尝试自动分配库存。由于我不是一个经验丰富的程序员,我很难创建逻辑。
目标是合并两个 Excel 文件并添加一个包含负责人/部门的列。到目前为止,我所做的是合并 Excel 文件并添加“Reviser”列。现在此列必须填写正确的人员/部门。
这背后的逻辑不是很困难,但我真的不知道如何用 Python/Pandas 等来做到这一点。 我已经尝试使用 np.where 解决此问题,但这并不能完全解决问题。
这里可以看到revisor赋值背后的逻辑:
[赋值逻辑][1]
感谢您的帮助!
我当前的代码:
import pandas as pd
import numpy as np
from openpyxl import Workbook
Q_Stock = pd.read_excel("C:\\Users\\Lucas\\Desktop\\Excel_Test\\Q Bestand.xlsx",usecols=["Bestandsqualifikation", "Inhalt", "Benutzerfeld 1", "Benutzerfeld 2","Material", "Externer Barcode 2", "Handling Unit"])
""" Q_Bestand["Bearbeiter"] = "" """
Q_Stocknew = Q_Stock[0:-1]
S_Stock = pd.read_excel("C:\\Users\\Lucas\\Desktop\\Excel_Test\\S Bestand.xlsx",usecols=["Bestandsqualifikation", "Inhalt", "Benutzerfeld 1", "Benutzerfeld 2","Material", "Externer Barcode 2", "Handling Unit" ])
""" S_Bestand["Bearbeiter"] = "" """
S_Stocknew = S_Stock[0:-1]
complete_list = [S_Stocknew, Q_Stocknew]
Combined = pd.concat(complete_list)
df = pd.DataFrame
def bar(df):
if Combined['Inhalt'] ==np.nan:
return np.nan
elif str(Combined['Inhalt']).contains("QV"):
return "Distribution"
elif str(Combined['Inhalt']).contains("QP"):
return "Production"
elif (Combined['Benutzerfeld 2'] == "ruckschnitt") and (str(Combined['Material']).contains("^09")):
return "Person 1"
df["Reviser"] = Combined.apply(bar, axis = 1)
Combined.to_excel(r'C:\\Users\Lucas\\Desktop\\Excel_Test\\Test.xlsx', index = True)
现在抛出这个错误:
C:\Python\Code\venv\Scripts\python.exe C:/Python/Code/SAP_Automatisieren.py 回溯(最近一次通话最后): 文件“C:/Python/Code/SAP_Automatisieren.py”,第 29 行,在 df["Reviser"] = Combined.apply(bar, axis = 1) 文件“C:\Python\Code\venv\lib\site-packages\pandas\core\frame.py”,第 6878 行,在应用中 返回 op.get_result() 文件“C:\Python\Code\venv\lib\site-packages\pandas\core\apply.py”,第 186 行,在 get_result 返回 self.apply_standard() 文件“C:\Python\Code\venv\lib\site-packages\pandas\core\apply.py”,第 295 行,在 apply_standard 结果 = libreduction.compute_reduction( 文件“pandas_libs\reduction.pyx”,第 620 行,在 pandas._libs.reduction.compute_reduction 文件“pandas_libs\reduction.pyx”,第 128 行,在 pandas._libs.reduction.Reducer.get_result 文件“C:/Python/Code/SAP_Automatisieren.py”,第 19 行,在栏中 如果组合 ['Inhalt'] ==np.nan: 文件“C:\Python\Code\venv\lib\site-packages\pandas\core\generic.py”,第 1478 行,在 nonzero 引发价值错误( ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
import pandas as pd
import numpy as np
from openpyxl import Workbook
Q_Stock = pd.read_excel("C:\\Users\\Lucas\\Desktop\\Excel_Test\\Q Bestand.xlsx",usecols=["Bestandsqualifikation", "Inhalt", "Benutzerfeld 1", "Benutzerfeld 2","Material", "Externer Barcode 2", "Handling Unit"])
""" Q_Bestand["Bearbeiter"] = "" """
Q_Stocknew = Q_Stock[0:-1]
S_Stock = pd.read_excel("C:\\Users\\Lucas\\Desktop\\Excel_Test\\S Bestand.xlsx",usecols=["Bestandsqualifikation", "Inhalt", "Benutzerfeld 1", "Benutzerfeld 2","Material", "Externer Barcode 2", "Handling Unit" ])
""" S_Bestand["Bearbeiter"] = "" """
S_Stocknew = S_Stock[0:-1]
complete_list = [S_Stocknew, Q_Stocknew]
Combined = pd.concat(complete_list)
df = pd.DataFrame
def bar(Combined):
if Combined['Inhalt'] ==np.nan:
return np.nan
elif str(Combined['Inhalt']).contains("QV"):
return "Distribution"
elif str(Combined['Inhalt']).contains("QP"):
return "Production"
elif (Combined['Benutzerfeld 2'] == "ruckschnitt") and (str(Combined['Material']).contains("^09")):
return "Person 1"
df["Reviser"] = Combined.apply(bar, axis = 1)
错误:
C:\Python\Code\venv\Scripts\python.exe C:/Python/Code/SAP_Automatisieren.py 回溯(最近一次通话最后): 文件“C:/Python/Code/SAP_Automatisieren.py”,第 28 行,在 df["Reviser"] = Combined.apply(bar, axis = 1) 文件“C:\Python\Code\venv\lib\site-packages\pandas\core\frame.py”,第 6878 行,在应用中 返回 op.get_result() 文件“C:\Python\Code\venv\lib\site-packages\pandas\core\apply.py”,第 186 行,在 get_result 返回 self.apply_standard() 文件“C:\Python\Code\venv\lib\site-packages\pandas\core\apply.py”,第 295 行,在 apply_standard 结果 = libreduction.compute_reduction( 文件“pandas_libs\reduction.pyx”,第 620 行,在 pandas._libs.reduction.compute_reduction 文件“pandas_libs\reduction.pyx”,第 128 行,在 pandas._libs.reduction.Reducer.get_result 文件“C:/Python/Code/SAP_Automatisieren.py”,第 21 行,在 bar elif str(Combined['Inhalt']).contains("QV"): AttributeError: 'str' 对象没有属性 'contains'
【问题讨论】: