I have a CSV file where I cannot change the object data types to INT64, STR64, BOOL, FLOAT64, etc.
How can one solve this?
The data import from the CSV file looks like:
filepath = ‘filename.csv’
df = pd.read_csv(filepath, sep=’,’)
Already tried:
- df[‘col’].astype(str)
- pd.to_numeric(df[‘col’], errors=’coerce’)
- df[‘col’].astype(str).replace(‘nan’, ”)
- a helper function such as:
def convert_to_int(string, separator=’,’, errors=’coerce’):
try:
return int(string.replace(separator, ”))
except ValueError as e:
if errors == ‘coerce’:
return 0
if errors == ‘raise’:
raise ValueError(e)
if errors == ‘ignore’:
return string*
New contributor
dzky23 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.