Skip to content
Advertisement

Python DataFrame String replace accidently Returing NaN

I encounter a weird problem in Python Pandas, while I read a excel and replace a character “k”, the result gives me NaN for the rows without “K”. see below image

enter image description here

It should return 173 on row #4,instead of NaN, but if I create a brand new excel, and type the same number. it will work.

or if i use this code,

df = pd.DataFrame({ 'sales':['75.8K','6.9K','7K','6.9K','173','148']})
df

enter image description here

then it will works well. Why? please advise!

Advertisement

Answer

Try this:

df['nums'] = df['sales'].astype(str)
df['nums'] = pd.to_numeric(df['nums'].str.replace('K', ''))
Advertisement