edit columnnames that include duplicate special characters

Question:

I have some column names that include two question marks at different spaces e.g. ‘how old were you? when you started university?’ – i need to identify which columns have two question marks in. any tips welcome! thanks

data

df = pd.DataFrame(data={'id': [1, 2, 3, 4, 5], 'how old were you? when you started university?': [1,2,3,4,5], 'how old were you when you finished university?': [1,2,3,4,5], 'at what age? did you start your first job?': [1,2,3,4,5]})

expected output

df1 = pd.DataFrame(data={'id': [1, 2, 3, 4, 5], 'how old were you when you finished university?': [1,2,3,4,5]})
Asked By: b101

||

Answers:

If you want to get all columns that have more than one question mark, you can use the following:

[c for c in df.columns if c.count("?")>1]

Edit: If you want to replace the extra "?" but keep the ending "?", use this:

df.rename(columns = {c: c.replace("?", "")+"?" for c in df.columns if c.find("?")>0})

Answered By: not_speshal

One idea with list comprehension:

df = df[[c for c in df.columns if c.count("?") < 2]]
print (df)
   id  how old were you when you finished university?
0   1                                               1
1   2                                               2
2   3                                               3
3   4                                               4
4   5                                               5
Answered By: jezrael

You can use boolean-indexing:

x = df.loc[:, df.columns.str.count(r"?") < 2]
print(x)

Prints:

   id  how old were you when you finished university?
0   1                                               1
1   2                                               2
2   3                                               3
3   4                                               4
4   5                                               5
Answered By: Andrej Kesely
df = df.drop([col for col in df.columns if col.count("?")>1], axis=1)
Answered By: Anurag Dhadse