Error with lambda function in pandas

Amani

I have a data frame sp with a column named Status. The values in the Status column are either 'Done' or 'Waiting'. I need to change the values of the Status column using a lambda function, where a status of 'Done' is changed to 'A' and a status of 'Waiting' is changed to 'N'. This is how I tried to do it:

sp['Status'] = sp['Status'].apply(lambda x: x='A' if x=='Done' else x='N')

I then get the following error message:

sp['Status'] = sp['Status'].apply(lambda x: x='A' if x=='Done' else x='N')
                                                                     ^
SyntaxError: invalid syntax

Where am I doing wrong?

Martijn Pieters

You can't use assignment (a statement) inside a lambda (which only takes expressions).

The lambda should instead just return the new value:

sp['Status'] = sp['Status'].apply(lambda x: 'A' if x == 'Alive' else 'N')

The result of the expression in a lambda is always the return value.

Note that you just use Series.map() here instead:

sp['Status'] = sp['Status'].map({'Alive': 'A', 'Waiting': 'N'})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysterious Python Pandas lambda function error

From Dev

Pandas Lambda Function : attribute error 'occurred at index 0'

From Dev

Lambda function notation in Pandas

From Dev

pandas apply function with arguments no lambda

From Dev

applying a lambda function to pandas dataframe

From Dev

Parameters Validation error Lambda function

From Dev

Error while connecting lambda function to QProcess::error

From Dev

Pandas Apply lambda function null values

From Java

Pandas Dataframe add column with lambda function

From Dev

Applying a lambda function to a column got failed in pandas

From Dev

Vectorizing a very simple pandas lambda function in apply

From Dev

Pandas Lambda function not working as I would like

From Dev

Applying a lambda function to a column got failed in pandas

From Dev

What is wrong with this lambda function? Pandas and Python dataframe

From Dev

Pandas Apply lambda function null values

From Dev

Vectorizing a very simple pandas lambda function in apply

From Dev

lambda function with multiple ifs using pandas df

From Dev

what object gets extracted by lambda function in pandas?

From Dev

Compilation error on passing lambda expression to template function

From Dev

Quicksort in lisp shows lambda function error

From Dev

No matching member function error inside lambda expression?

From Dev

confused about error using lambda function

From Dev

C++11 Lambda function compilation error

From Dev

java 8 - some error with compiling lambda function

From Dev

Parse JSON error with Java And AWS Lambda function

From Dev

AWS Lambda function response type error

From Dev

Pandas - Apply function and generate more than one row with lambda function

From Dev

No matching function error when passing lambda function as argument

From Dev

C++ Lambda: Access static method in lambda leads to error 'this was not captured for this lambda function'

Related Related

  1. 1

    mysterious Python Pandas lambda function error

  2. 2

    Pandas Lambda Function : attribute error 'occurred at index 0'

  3. 3

    Lambda function notation in Pandas

  4. 4

    pandas apply function with arguments no lambda

  5. 5

    applying a lambda function to pandas dataframe

  6. 6

    Parameters Validation error Lambda function

  7. 7

    Error while connecting lambda function to QProcess::error

  8. 8

    Pandas Apply lambda function null values

  9. 9

    Pandas Dataframe add column with lambda function

  10. 10

    Applying a lambda function to a column got failed in pandas

  11. 11

    Vectorizing a very simple pandas lambda function in apply

  12. 12

    Pandas Lambda function not working as I would like

  13. 13

    Applying a lambda function to a column got failed in pandas

  14. 14

    What is wrong with this lambda function? Pandas and Python dataframe

  15. 15

    Pandas Apply lambda function null values

  16. 16

    Vectorizing a very simple pandas lambda function in apply

  17. 17

    lambda function with multiple ifs using pandas df

  18. 18

    what object gets extracted by lambda function in pandas?

  19. 19

    Compilation error on passing lambda expression to template function

  20. 20

    Quicksort in lisp shows lambda function error

  21. 21

    No matching member function error inside lambda expression?

  22. 22

    confused about error using lambda function

  23. 23

    C++11 Lambda function compilation error

  24. 24

    java 8 - some error with compiling lambda function

  25. 25

    Parse JSON error with Java And AWS Lambda function

  26. 26

    AWS Lambda function response type error

  27. 27

    Pandas - Apply function and generate more than one row with lambda function

  28. 28

    No matching function error when passing lambda function as argument

  29. 29

    C++ Lambda: Access static method in lambda leads to error 'this was not captured for this lambda function'

HotTag

Archive