Skip to content
Advertisement

Apply function to all columns of data frame python

I have two dfs

JavaScript
AVERAGE_CALL_DURATION AVERAGE_DURATION CHANGE_OF_DETAILS
267 298 0 0
421 609.33 0.33
330 334 0 0
240.5 666.5 0
628 713 0 0

and

JavaScript
AVERAGE_CALL_DURATION AVERAGE_DURATION CHANGE_OF_DETAILS
-5.93 -4.95 0.90
593.50 595.70 1.00

I want to return 1 if the xx column contains the range within NoC_c (where column names are the same

I can do this for one column

JavaScript

However, I need remove the element of manually specifying the column name as the actual dfs contain many more columns.

I have tried

JavaScript

However, I get the error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried the solutions listed here, although, they were unsuccessful

Also read this to address the specific error but didn’t aid in my issue

Any help would be appreciated.

JavaScript

Edit:: Many thanks to @jch for the solution. I’m re-posting here as I had to modify some of the syntax for it to work with my datasets

JavaScript

Advertisement

Answer

Would this work for you?

Comparison Function

JavaScript

Execution

JavaScript

Result

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement