astro
astro

Reputation: 39

Pandas replace and fillna if column begins with part of a string

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!

Upvotes: 1

Views: 397

Answers (2)

user16836078
user16836078

Reputation:

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)

Upvotes: 2

Shubham Sharma
Shubham Sharma

Reputation: 71689

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)

Upvotes: 1

Related Questions