I was wondering whether there is a way to use the .replace() and .fillna() for columns headers that begin with part of a string.
For example:
data['Q5_A'].fillna(value='Not Applicable', inplace=True)
This will only apply .fillna() to column Q5_A.
Similarily:
data["Q5_S"] = data["Q5_S"].replace(",", "", regex=True)
This will only apply .replace() to column Q5_S.
What if I would like it to apply to any columns that have "Q5_" in the header?
Thanks!
CodePudding user response:
You can do a for loop and perform all the processes you need as well.
for header in data.columns:
if "Q5" in header:
data[header].fillna(value='Not Applicable', inplace=True)
data[header] = data[header].replace(",", "", regex=True)
CodePudding user response:
Filter
the required columns then do replace/fillna:
data.assign(**data.filter(like='Q5_').replace(",", "", regex=True))
Alternatively, if you want to modify the dataframe inplace, we can do:
cols = data.filter(like='Q5_').columns
data[cols] = data[cols].replace(",", "", regex=True)