Skip to content
Advertisement

Apply a function including if to each row of a dataframe in pandas without for loop

Given a dataframe, I want to get the nonzero values of each row and then find the minimum of absolute values. I want to have a user defined function that does this for me. Also, I do not want to use any for loop since the data is big.

My try

JavaScript

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

Question How can I solve the above?

The desired answer is the following:

JavaScript

Advertisement

Answer

You can use x.ne(0) as boolean indexing to filter row

JavaScript
JavaScript

Or use min(axis=1)

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