Skip to content Skip to sidebar Skip to footer

Convert An Object Dtype Column To Number Dtype In A Datafrane Pandas

Trying to answer this question Get List of Unique String per Column we ran into a different problem from my dataset. When I import this CSV file to the dataframe every column is OB

Solution 1:

Option 1 use pd.to_numeric in an apply

df.apply(pd.to_numeric, errors='ignore')

Option 2 use pd.to_numeric on df.values.ravel

cvrtd = pd.to_numeric(df.values.ravel(), errors='coerce').reshape(-1, len(df.columns))
pd.DataFrame(np.where(np.isnan(cvrtd), df.values, cvrtd), df.index, df.columns)

Note These are not exactly the same. For some column that contains mixed values, option 2 converts what it can while option 2 leaves everything in that column an object. Looking at your file, I'd choose option 1.


Timing

df = pd.read_csv('HistorianDataSample/HistorianDataSample.csv', skiprows=[1, 2])

enter image description here

Post a Comment for "Convert An Object Dtype Column To Number Dtype In A Datafrane Pandas"