0

I am trying to change the datatype of column '2019' from object to int64 which also contains empty strings so I can perform mathematical operations on them, I tried using to_numeric but I keep on getting a value error of this

Traceback (most recent call last):
File "pandas\_libs\lib.pyx", line 2062, in 
pandas._libs.lib.maybe_convert_numeric
ValueError: Unable to parse string "   "

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "C:\Users\user\open csv.py", line 22, in <module>
 df['2019']= pd.to_numeric(df['2019'])
 File"C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\core\tools\numeric.py", line 155, in to_numeric
values, set(), coerce_numeric=coerce_numeric

File "pandas_libs\lib.pyx", line 2099, in pandas._libs.lib.maybe_convert_numeric ValueError: Unable to parse string " " at position 103

This is my code, please can anyone help me convert it and remove the empty strings too?

import pandas as pd
import numpy as np

df = pd.read_csv('seun.csv')

##df = df.astype(str)

##df.replace("-","")

##df.to_csv('seun.csv', index = False, header=True)

print(df.to_string())

a= df.loc[2, '2019']
b= df.loc[3, '2019']

print(df.to_numeric())
##f= (df[pd.to_numeric(df['2019'], errors='coerce').isnull()])
df['2019']= pd.to_numeric(df['2019'])
##df['2019'].replace('', np.nan, inplace=True)
##df.dropna(subset=['2019'], inplace=True)
print(df.dtypes)

df sample

S/NO    HOUSE OWNERS NAME       DATE        2019
1        MOBS INCORP        2019-04-22   100,000.00     
2        SEUN HOUSES        2020-05-03   100,000.00     
3                                        50,000.00      
4        SEUN HOUSES        2020-05-03   50,000.00      
5                                        100,000.00     
6                                   
7                                        50,000.00
8       CARSON VENTURE      2020-07-02                   
9       WONDERFUL LIMITED   2020-09-05
10       SEUN HOUSES        2020-05-03   50,0000.00                     
Seun
  • 1
  • 1

0 Answers0