Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove "?" from python data frame field

Tags:

python

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

like image 851
Sonia Avatar asked Aug 27 '26 18:08

Sonia


2 Answers

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
like image 110
Rakesh Avatar answered Aug 29 '26 08:08

Rakesh


Try using inplace=True to keep changes in your original dataframe.

data.replace('?','NaN', inplace=True)
like image 28
NZ_DJ Avatar answered Aug 29 '26 08:08

NZ_DJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!