简体   繁体   中英

Apply an if/then condition over all elements of a Pandas dataframe

I need to apply a very simple if/then function to every element in a Pandas Dataframe.

If the value of any element is over 0.5, I need to return a 1. Otherwise, I need to return a 0.

This seemed really simple with a lambda function, but every time I try I get an error: 'ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()' 'ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()'

So far I have:

df_new = df.apply(lambda x: 1 if x > 0.5 else 1)

I'd be grateful for any help.

You should use applymap instead because you want the operation to be performed on each element in your dataframe, not each column, which is what apply does.

df = pd.DataFrame({"A": [0.1, 0.2, 0.5, 0.6, 0.7],
                  "B": [0.75, 0.85, 0.2, 0.9, 0.0],
                  "C": [0.2, 0.51, 0.49, 0.3, 0.1]})

print(df)

      A        B       C
0   0.1     0.75    0.20
1   0.2     0.85    0.51
2   0.5     0.20    0.49
3   0.6     0.90    0.30
4   0.7     0.00    0.10

df_new = df.applymap(lambda x: 1 if x > 0.5 else 0)

print(df_new)

    A   B   C
0   0   1   0
1   0   1   1
2   0   0   0
3   1   1   0
4   1   0   0

If it is something binary, you could use the following:

df = (df > 0.5).astype(int)

The code in the parentheses would generate a dataframe of bools, which would then be converted to the ints 1 and 0 (1 for True, 0 for False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM