0

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*
2
  • The file can be found here: h-t-t-p-s-://easyupload.io/r7c4pm Commented Aug 6, 2024 at 7:57
  • When you tried df['col'].astype(str) you will have seen a KeyError exception because there is no column named "col" Commented Aug 6, 2024 at 8:13

1 Answer 1

0

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')
Sign up to request clarification or add additional context in comments.

4 Comments

Hello sergiomahi, I want to change an object column containing only chars and numbers to STR (e.g. STR64) format. I snipped the code above, it was not executed like that. The original code regards your recommendation regarding 'inplace'. I used this format for better readability, sorry for the confusion.
Noted. There is no STR64 in pandas, as I mentioned the strings types are usually managed as object. The closest thing you can do is transform to 'string'. See my edit above. @dzky23
Ok, understood, but that does not help. I used: df['icao24'] = df['icao24'].astype(str) To my understanding, this should work with an OBJECT data type column, but it does not. Data type is still OBJECT after conversion.
@dzky23 it has to be astype('string'). I won't work with astype(str)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.