Home > OS >  How print dataframe column names that have a particular value?
How print dataframe column names that have a particular value?

Time:10-31

I'm currently doing this for each column:

df['slope'].isin(['?'])

How do I print all columns that have at least one value '?'?

Data set looks like this:

age sex cp  trestbps    chol    fbs restecg thalach exang   oldpeak slope   ca  thal    target
0   28.0    1.0 2.0 130 132 0   2   185 0   0   ?   ?   ?   0

I'm looking for a function that will print slope,ca,thal (the ones that contains '?')

CodePudding user response:

you can use:

df= df.loc[: , (df == '?').any()]
#or columns

col_list=df.loc[: , (df == '?').any()].columns.to_list()

#if you are looking "isin"
df= df.loc[: , df.apply(lambda x: x.str.contains('?').any(),axis=0)]


CodePudding user response:

df = df[[x for x in df.columns if any(df[x].astype(str).str.contains(r"\?"))]]

CodePudding user response:

You can do it like this:

for col in df.columns:
    if df[col].astype('str').str.contains(r'\?').any():
       print(col)
  • Related