Home > OS >  Pandas: Extract columns with ALL Rows > 0
Pandas: Extract columns with ALL Rows > 0

Time:10-17

Using pandas=1.1.5. I just want to extract the columns that have above 0 for all rows. Thank you

df1

      | Revenue |  Profit   | Sales |
0      |  0    |  300      |  1    |
1      |  500    |  900      |  3    |
2      |  200    |  100      |  4    |

Desired Outcome as Revenue has 0 in 1st row, exclude it

       |  Profit   | Sales |
0      |   300      |  1    |
1      |   900      |  3    |
2      |  100      |  4    |

CodePudding user response:

You can use (df > 0).all() to find the columns that have all values greater than zero:

df.loc[:, (df > 0).all()]

   Profit  Sales
0     300      1
1     900      3
2     100      4

CodePudding user response:

I think there must be more proper way to do that, but you can try code below:

for col in df.columns:
    df[col] = df[col].apply(lambda x: x if x > 0 else None)
df.dropna(axis='columns')

Profit  Sales
0   300     1
1   900     3
2   100     34
  • Related