I have the following dataframe: dataframe
I got the data by scrapping a basketball website but all the datatypes of the columns are objects instead of floats. I tried but splitting up the dataframe to only the numeric columns to do .astype(float) but I get an error. I just need help converting the proper columns into floats instead of objects.
Can't use .astype(float) cause the column has numbers that start with "." column example
How to remove the extra 0's screenshot
CodePudding user response:
This will allow you to change specific columns into a float
df[['column1', 'column2']] = df[['column1', 'column2']].astype(float)
CodePudding user response:
Use this pd.to_numeric with errors='ignore':
df.apply(pd.to_numeric, errors='ignore')
Here is a MCVE:
df = pd.DataFrame({'Letters':[*'ABCD'],
'Numbers':[1,2,3,4],
'Decimals':[.1,.2,.3,.4],
'String Numbers':['1', '.2', '1.5', '.04'],
'Words':'Dog Jane Tom Cat'.split(' ')})
df.apply(pd.to_numeric, errors='ignore')
Output:
Letters Numbers Decimals String Numbers Words
0 A 1 0.1 1.00 Dog
1 B 2 0.2 0.20 Jane
2 C 3 0.3 1.50 Tom
3 D 4 0.4 0.04 Cat
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Letters 4 non-null object
1 Numbers 4 non-null int64
2 Decimals 4 non-null float64
3 String Numbers 4 non-null float64
4 Words 4 non-null object
dtypes: float64(2), int64(1), object(2)
memory usage: 288.0 bytes