Issue
I have a dataframe that has many instances of '?' in different rows with dtype 'object'.
How can I replace all the '?' with 0.
Solution
Consider the dataframe df
df = pd.DataFrame([['?', 1], [2, '?']])
print(df)
0 1
0 ? 1
1 2 ?
replace
df.replace('?', 0)
0 1
0 0 1
1 2 0
mask
or where
df.mask(df == '?', 0)
# df.where(df != '?', 0)
0 1
0 0 1
1 2 0
However, imagine your dataframe has ?
within longer strings.
df = pd.DataFrame([['a?', 1], [2, '?b']])
print(df)
0 1
0 a? 1
1 2 ?b
replace
with regex=True
df.replace('\?', '0', regex=True)
0 1
0 a0 1
1 2 0b
Answered By - piRSquared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.