How print dataframe column names that have a particular value?

Question:

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 '?')

Asked By: jimmy

||

Answers:

You can do it like this:

for col in df.columns:
    if df[col].astype('str').str.contains(r'?').any():
       print(col)
Answered By: salman

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)]


Answered By: Clegane
df = df[[x for x in df.columns if any(df[x].astype(str).str.contains(r"?"))]]
Answered By: Jason Baker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.