[Python] How to get the output values ​of a apply

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have a function used by an application which returns two str. I want to assign the output values to two columns of a dataframe. But I don't understand how to assign to get the values, I get NaN values.

    import pandas as pd
    df = pd.DataFrame({
    'nombre': [10, 25, 42, 18, 73]
    }); df["COMMENT"] = df["S"] = None

    def verif(row):
    if row["nombre"] % 10 == 0: c = "Ends with 0"; e = "OK"
    else: c = "Do not end with 0"; e = "KO"
    return pd.Series({"e": e, "c": c})

    mask = df["nombre"] % 2 == 0
    df.loc[mask, ["S", "COMMENT"]] = df.loc[mask].apply(verif, axis=1)

    Continue reading...

Compartilhe esta Página