pandas


Convert multiple datatype to float?


Using pandas, how to convert multiple dateframe column of datatype "object" to float.
df = pd.DataFrame()
df["A"] = ["123.45","34","-9","4","5"]
df["B"] = ["-9.07","5.4","3","1.0","4.5557"]
df["C"] = ["34","34.98","-9.654","45","6"]
df["D"] = ["AAA","AVF","ERD","DFE","SFE"]
using this gives AttributeError: 'list' object has no attribute 'apply':
[df["A"],df["B"],df["C"]] = [df["A"],df["B"],df["C"]].apply(pd.to_numeric, errors='coerce')
df = df.apply(pd.to_numeric, errors='coerce')
In [119]: df
Out[119]:
A B C
0 123.45 -9.0700 34.000
1 34.00 5.4000 34.980
2 -9.00 3.0000 -9.654
3 4.00 1.0000 45.000
4 5.00 4.5557 6.000
In [120]: df.dtypes
Out[120]:
A float64
B float64
C float64
dtype: object
UPDATE:
In [128]: df[df.columns.drop('D')] = df[df.columns.drop('D')].apply(pd.to_numeric, errors='coerce')
In [129]: df
Out[129]:
A B C D
0 123.45 -9.0700 34.000 AAA
1 34.00 5.4000 34.980 AVF
2 -9.00 3.0000 -9.654 ERD
3 4.00 1.0000 45.000 DFE
4 5.00 4.5557 6.000 SFE
In [130]: df.dtypes
Out[130]:
A float64
B float64
C float64
D object
dtype: object
UPDATE2:
In [143]: df[['A','B','C']] = df[['A','B','C']].apply(pd.to_numeric, errors='coerce')
In [144]: df
Out[144]:
A B C D
0 123.45 -9.0700 34.000 AAA
1 34.00 5.4000 34.980 AVF
2 -9.00 3.0000 -9.654 ERD
3 4.00 1.0000 45.000 DFE
4 5.00 4.5557 6.000 SFE
In [145]: df.dtypes
Out[145]:
A float64
B float64
C float64
D object
dtype: object

Related Links

how to convert pandas dataframe to libsvm format?
Merging two dataframes based on a date between two other dates without a common column
Why I can't change the series format?
Copy numpy array into Panda multiindex (same size)
Average Previous and Current Row - Pandas
seaborn pointplot above swarmplot
Edit field and append value to a python dataframe column
column_stack returns non cotiguous array
pandas: conditionally select a row cell for each column based on a mask
pandas custom function apply on melted dataframe
How to check for boolean codition in pandas dataframe
Reading batches of data from BigQuery into Datalab
Jupyter/ipywidgets sorting dataframe on two levels
Groupby.sum() giving ValueError: overflow in timedelta operation
Why does DataFrameGroupBy.boxplot method throw error when given argument “subplots=True/False”?
Calculate age in months - optimize date transformations in pandas

Categories

HOME
tizen
adb
sharepoint-designer
windows-server
acquia
opengl-es-2.0
convolution
swagger-ui
paradox
append
tomcat6
volttron
modx-revolution
hex-editors
vaadin7
seaborn
graphlab
django-admin
code-review
reverse-proxy
ml
xlsxwriter
physics-engine
functional-testing
telerik-reporting
android-widget
centos6.5
semantic-analysis
fgetcsv
buck
vision
instant-messaging
pdb
repo
openoffice.org
revolution-slider
appfabric
neuroscience
ibpy
total-commander
acoustics
sencha-touch-2.3
qcombobox
convertapi
azure-application-gateway
zip4j
worksheet
ionicons
return-value
idisposable
magma
font-size
boost-preprocessor
sqldf
gnome-shell-extensions
gridpane
rotativa
multi-level
moveit
rvest
fadeout
xpath-1.0
phpcas
retina
wordml
citrus-pay
iis-arr
0xdbe
sailfish-os
c++03
npapi
kineticjs
c3
gulp-less
htmlcleaner
commoncrypto
cdc
listings
plasma
robotics-studio
angularjs-controller
typoscript2
simba
chuck
cisco-jtapi
labwindows
ecl
gamma
getresponsestream
bespin
thunderbird-lightning
database-management
web-architecture
microsoft-virtualization
avatar
xetex
temporal-database
putchar

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App