0

I've tried searching this on Stack Overflow but don't seem to be able to find the same scenario. I'm trying to update multiple columns in my pandas dataframe in one line of code with same function. I can update each column individually, but want the function to update multiple columns simultaneously.

The function is basic:

binary_values = lambda x: 0 if x == -99 else 1

I know I can assign scalar values to sections of the dataframe like this:

df.ix[:,22:26] = 1

I'm trying to do something like below:

df.ix[:,22:26] = df.ix[:,22:26].apply(binary_values)

...but it throws up ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index..')

1 Answer 1

1

Your lambda function is not vectorized, you need applymap to apply it to each element of your data frame:

df.iloc[:,22:26] = df.iloc[:,22:26].applymap(binary_values)

Example:

df = pd.DataFrame({
    "A":[1,2,3,-99,5,3],
    "B":[2,3,5,2,-99,2],
    "C":[-99,3,2,1,4,5]
})

binary_values = lambda x: 0 if x == -99 else 1
df.iloc[:,1:] = df.iloc[:,1:].applymap(binary_values)

df

#   A   B   C
#0  1   1   0
#1  2   1   1
#2  3   1   1
#3  -99 1   1
#4  5   0   1
#5  3   1   1

A simpler (vectorized) option is check the equality and then convert the data type to int:

df.iloc[:,1:] = df.iloc[:,1:].ne(-99).astype(int)

df
#   A   B   C
#0  1   1   0
#1  2   1   1
#2  3   1   1
#3  -99 1   1
#4  5   0   1
#5  3   1   1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.