Home > Mobile >  How do I apply conditional statements to multiple columns on Pandas Dataframe using iloc?
How do I apply conditional statements to multiple columns on Pandas Dataframe using iloc?

Time:02-17

Assuming I have a dataframe like this:

# importing pandas and numpy
import pandas as pd
import numpy as np

# create a sample dataframe
data = pd.DataFrame({
    'A' : [ 1, 2, 3, -1, -2, 3],
    'B' : [ -1, -2, -3, 12, -12, -3],
    'C' : [ 1, -2, 3, -1, -2, 13],
    'D' : [ -1, 2, 3, -1, 2, -3],
    'E' : [ 1, 12, 3, 11, -2, 3]
})

# view the data
data

This gives:

   A   B   C  D   E
0  1  -1   1 -1   1
1  2  -2  -2  2  12
2  3  -3   3  3   3
3 -1  12  -1 -1  11
4 -2 -12  -2  2  -2
5  3  -3  13 -3   3

Now, my question is how can I apply an if statement onto all the columns and rows, such that it results all the values of the table being replaced by the if statement values.

The equation I want to apply is:

if x <= 0:
    0
else:
    x

So the resultant data will eliminate all the negative values in this case:

   A   B   C  D   E
0  1   0   1  0   1
1  2   0   0  2  12
2  3   0   3  3   3
3  0  12   0  0  11
4  0   0   0  2   0
5  3   0  13  0   3

I tried using the following loc and applied them to A, B, C, D and E:

data.loc[(data.A <= 0), ['A']] = 0
data.loc[(data.A > 0), ['A']] = data.A

But it is not an optimal solution as my columns might have more than A, B, C, D and E. Also, sometimes I will need to solve for other inequalities.

Any guidance will be much appreciated.

Thank you!

CodePudding user response:

Use DataFrame.clip with lower:

df = data.clip(lower=0)
print (df)
   A   B   C  D   E
0  1   0   1  0   1
1  2   0   0  2  12
2  3   0   3  3   3
3  0  12   0  0  11
4  0   0   0  2   0
5  3   0  13  0   3

For only some columns:

cols = ['A','B','C']
data[cols] = data[cols].clip(lower=0)
print (data)
   A   B   C  D   E
0  1   0   1 -1   1
1  2   0   0  2  12
2  3   0   3  3   3
3  0  12   0 -1  11
4  0   0   0  2  -2
5  3   0  13 -3   3
  • Related