【问题标题】:Adding Data to column based on content of other cells (Python/Excel)根据其他单元格的内容将数据添加到列(Python/Excel)
【发布时间】: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'

【问题讨论】:

    标签: python excel pandas numpy


    【解决方案1】:

    您可以编写如下所示的函数并将其应用于您的数据框。您也可以编写一个嵌套的np.where,但对于来自 Excel 世界的您来说,下面的函数将更具可读性

    def bar(df):
        if df['Inventory Qualifikation'] ==np.nan:
            return np.nan
        elif str(df['Inventory Qualifikation']).contains("QV"):
            return "Distribution"
        elif str(df['Inventory Qualifikation']).contains("QP"):
            return "Production"
        elif (df['Userfield 2'] == "ruckschnitt") and (str(df['material']).contains("^09")):
        return "Person 1"
    
    df['reviser'] = df.apply(bar, axis = 1)
    

    【讨论】:

    • 感谢您的帮助。但我仍然遇到一些错误。所以“def”=说我想定义一个函数,bar =函数名称,(df)=我想要函数对齐的数据框?我添加了我的新代码..
    • 您遇到的错误是什么?你能把错误复制粘贴到这里吗?
    • 是的,我在新代码下方添加了它。评论区太长了..
    • 我现在编辑了答案。你能试试编辑后的版本吗?请注意我改变了df[].str to str(df[])
    • 非常感谢。但我想我错过了一些东西:python中的函数以“def”开头,然后是“bar”这个名字。哪个数据框在“bar”后面的括号中?函数内的数据框,如“if df [”库存资格”] 是包含“库存资格”列的数据框。(在我的情况下:complete_list?)。然后是函数之外的最后一个数据框“df [ "reviser"] =..." 这会给我一个带有 "reviser" 列的新数据框,对吗?我必须将它应用到我已经存在的 complete_list df 中?
    猜你喜欢
    • 2022-12-09
    • 2014-09-13
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多