Reputation: 5097
I am loading a .csv into a dataframe (df) an am converting columns one at a time to numeric:
df = pd.read_csv(fullPath, converters={'Total requests received': lambda x: pd.to_numeric(x, errors='coerce'),
'Requests Processed': lambda x: pd.to_numeric(x, errors='coerce'),
'Requests still being processed': lambda x: pd.to_numeric(x, errors='coerce')})
Is there a way to convert all columns in one go to numeric and not have to name them one by one?
Upvotes: 0
Views: 73
Reputation: 91
Had this same question recently. You should be able to use dtype to specify when you read the csv. Check out this question and the code below: Pandas reading csv as string type
import pandas as pd
# this will make everything a float, so no truncation occurs
df = pd.read_csv(file_path, dtype=float)
# or if you aren't afraid of truncation
df = pd.read_csv(file_path, dtype=int)
The caveat is that this solution will attempt to read every column as int or float (though you can specify dtype as a dict {col: dtype}, but that isn't your question). https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
If you want numpy types check out these: https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html.
Alternatively, @Harv Ipan's answer is great too, and will accomplish the same objective. Albeit with an additional iteration.
Lastly, consider if you actually need this or not. Pandas generally does a good job at interpreting type, but I assume you have some specific use case.
Upvotes: 2