How to prevent pandas.read_csv()
from inferring the data types. For example, its converting strings true
and false
to Bool: True
and False
. The columns are many for many files, therefore not feasible to do:
df['field_name'] = df['field_name'].astype(np.float64)
for each of the columns in each file. I prefer pandas to just read file as it is, no type inferring.
Importing csv files in Python is 100x faster than Excel files. We can now load these files in 0.63 seconds. That's nearly 10 times faster! Python loads CSV files 100 times faster than Excel files.
We can use the parse_dates parameter to convince pandas to turn things into real datetime types. parse_dates takes a list of columns (since you could want to parse multiple columns into datetimes ).
Method 1: Use the index = False argument But you should also include index = False argument. It will automatically drop the unnamed column in pandas. And if you want to set the index for the dataframe then you can call the df.
Use the parameter dtype=object
for Pandas to keep the data as such at load time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With