Jui Sen
Jui Sen

Reputation: 377

Alternative options to replace column values

I have a data frame:

df = pd.DataFrame({"id": [1,2,3], "2000": ['p', 'w', nan], "2001": ['p', 'p', nan], "2002": ['p', nan, 'p']})

id  2000 2001 2002
1    p    p     p
2    w    p     nan
3    nan  nan   p

I want to replace all the 'p' and 'w' by '1'.

I applied :

df['2000'] = df['2000'].str.replace('p','1')
df['2000'] = df['2000'].str.replace('w','1')

df['2001'] = df['2001'].str.replace('p','1')
df['2001'] = df['2001'].str.replace('w','1')

and so on for each year and it is working.

id  2000 2001 2002
1    1    1     1
2    1    1     nan
3    nan  nan   1


Since, I have a very long data, I am wondering whether there any alternative command, that I can use for the entire data-frame, instead of coding for each column.

Upvotes: 0

Views: 250

Answers (1)

Corralien
Corralien

Reputation: 120439

>>> df.replace({r'[pw]': 1}, regex=True)
   id  2000  2001  2002
0   1   1.0   1.0   1.0
1   2   1.0   1.0   NaN
2   3   NaN   NaN   1.0

Upvotes: 4

Related Questions