Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string based NaN's to numpy NaN's

I have a dataframe with a part of it shown as below:

2016-12-27              NaN
2016-12-28              NaN
2016-12-29              NaN
2016-12-30              NaN
2016-12-31              NaN
Name: var_name, dtype: object

The column contains NaN as strings/objects. How can I convert it to a numpy nan instead. Best would be able to do so when I read in the csv file.

like image 562
user308827 Avatar asked Jul 30 '26 08:07

user308827


2 Answers

df[var_name_replace] = df[var_name].replace('NaN', pd.NA)

This simply replaces the 'NaN' string object with pd.NA. This uses the top-level .replace(), not the string replace (that is, NOT .str.replace()).

You could use np.nan in the place of pd.NA and it would work nearly the same way.

When you first load a file, Pandas will usually do these sorts of conversions by default. But if you know just a specific form of the string is in your object at some later point in the program, this is a good way to accomplish the conversion.

like image 64
GH KIM Avatar answered Jul 31 '26 23:07

GH KIM


I'd use the converters option in read_csv. In this case, we are aiming to convert the column in question to numeric values and treat everything else as numpy.nan which includes string version of 'NaN'

converter = lambda x: pd.to_numeric(x, 'coerce')
df = pd.read_csv(StringIO(txt), delim_whitespace=True, converters={1: converter}, header=None)
df

enter image description here

df.dtypes

0     object
1    float64
dtype: object
like image 36
piRSquared Avatar answered Jul 31 '26 22:07

piRSquared



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!