Comment dépanner les erreurs de nom lors de l'application de fonctions à plusieurs colonnes dans Pandas ?

Linda Hamilton
Libérer: 2024-10-18 07:30:30
original
120 Les gens l'ont consulté

How to Troubleshoot NameErrors When Applying Functions to Multiple Columns in Pandas?

Troubleshooting Pandas 'apply' Function with Multiple Column Referencing

In an attempt to apply a custom function to multiple columns in a Pandas DataFrame, the 'apply' function is encountering a NameError.

The error message, "global name 'a' is not defined," indicates that the 'a' variable is not accessible within the function. Upon closer examination, it emerges that the column name should be enclosed in quotes, as in 'row['a']'.

The corrected code should look like this:

<code class="python">df['Value'] = df.apply(lambda row: my_test(row['a'], row['c']), axis=1)</code>
Copier après la connexion

However, even after resolving this syntax error, the code still fails when using a more complex function. This suggests a different issue.

A critical step in the provided function is to iterate through the DataFrame's index and compare the parameter 'a' with each value in column 'a'. To access these elements, the syntax should be adjusted as follows:

<code class="python">def my_test(a):
    cum_diff = 0
    for ix in df.index:
        cum_diff += (a - df['a'][ix])
    return cum_diff</code>
Copier après la connexion

By incorporating these corrections, the code should now function as expected.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!