dzky23
dzky23

Reputation: 1

Change Datatype of CSV Import with Python/Pandas

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:

Upvotes: 0

Views: 79

Answers (1)

sergiomahi
sergiomahi

Reputation: 992

It depends on what columns you want to change their type. You have to know that, for example you can't change a string column (with characters) into a numeric one. If you run df.dtypes you can see the type of each column.

Additionally you have to bear in mind that there is no str type in pandas, they are usually named as object.

Lastly, the methods you showed, like df['col'].astype(str), don't work inplace, which means that they return a new serie instead of modifiying df directly. You should do:

df['col'] = df['col'].astype('string')

You can share a list of the columns and the desired type and we could help you with the right code to achieve it.

Edit.

If you want to change all object columns to string you can do:

for col in df.columns.tolist():
  if df[col].dtype != 'object':
    continue
  df[col] = df[col].astype('string')

Upvotes: 0

Related Questions