I have a python dataframe,named data. In this data frame one field is price with data type object.
The data frame contains missing value in different fields & missing values are "?". I want to replace that missing value. I thought to replace "?" by "NaN" & then apply fillna(). So I wrote this code
data.replace('?','NaN')
But after executing this code,I'm still getting "?" while I'm writing below line
data['price'].max()
Can you suggest me what is the correct way? i'm using python 3.6 on Windows 10
Try using np.nan
Ex:
import pandas as pd
import numpy as np
data = pd.DataFrame({"price": ["?", "Hello", "?", "World"]})
data["price"].replace('?',np.nan, inplace=True)
print(data.fillna(0))
Output:
price
0 0
1 Hello
2 0
3 World
Try using inplace=True to keep changes in your original dataframe.
data.replace('?','NaN', inplace=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With