Search code examples
pythondataframedrop

Drop column of pd dataframe if one specific entry is 0


I'm trying to drop columns from a df in which one spec. entry is 0. So in the pic I wont to drop Loan F to Loan P cause the entries in row zero is 0. Can anyone help here? Thx!

enter image description here


Solution

  • To do this, iterate through each column, see if the first row (assume that is where you are looking for the zero) value is equal to 0 and delete that column if true.

    for col in df.columns:
        if df[col].iloc[0] == 0:
            df.drop(col, axis=1, inplace=True)