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
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,
JavaScript
x
3
1
df = pd.DataFrame({ 'sales':['75.8K','6.9K','7K','6.9K','173','148']})
2
df
3
then it will works well. Why? please advise!
Advertisement
Answer
Try this:
JavaScript
1
3
1
df['nums'] = df['sales'].astype(str)
2
df['nums'] = pd.to_numeric(df['nums'].str.replace('K', ''))
3