Maison  >  Article  >  développement back-end  >  Comment dépanner les erreurs de nom lors de l'application de fonctions à plusieurs colonnes dans Pandas ?

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

Linda Hamilton
Linda Hamiltonoriginal
2024-10-18 07:30:30121parcourir

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>

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>

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!

Déclaration:
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