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 can I access multiple columns in Pandas 0.15 DataFrame.resample method?
Reconstruct a categorical variable from dummies in pandas
Python folium GeoJSON map not displaying
Efficient way to clean a csv?
SKlearn metrics fails with expected y object and predicted y object
Tableau - blend, join, or modify raw?
Pandas Dataframe reindexing issue
pandas TimeStamp subtraction?
Convert string to integer pandas dataframe index
Pandas.dataframe.read_table() ignores my row labels
pandas multiindex selection with ranges
How to prefer Series over DataFrame
any demo for statsmodels regression model in crossvalidation setting?
Read --> Modify -->Write large .csv files with Pandas
How do you filter out rows with NaN in a panda's dataframe
Pandas: How to grab unique values from a group?

Categories

HOME
log4j
deezer
fft
lodash
cplex
leon
infragistics
youtube-api-v3
acquia
uber-api
spring-xd
imacros
leiningen
uitypeeditor
clojurescript
foselasticabundle
fatal-error
footer
zapier
tar
captiveportal
crosstab
su
poltergeist
chromebook
info.plist
pingfederate
fluentvalidation
vsts-build-task
avcapturesession
host
web-mining
space-complexity
swift3.0.2
libraries
android-browser
sharefile
theano.scan
az-application-insights
hue
streamreader
leading-zero
azure-ml
galen
heightmap
node-sass
btrace
outlook-2013
komodoedit
jquery-validate
worksheet
knockout-components
pg-dump
media-player
boost-preprocessor
windows-mobile-6.5
spring-mongodb
django-scheduler
or-tools
firmata
heidisql
underscore.js-templating
mikroc
xml-attribute
autorest
bstr
gridview-sorting
qcustomplot
ptrace
intel-fortran
pundit
tarjans-algorithm
wyam
android-listview
bridge.net
tablelayout
android-nested-fragment
qcodo
issuu
terminfo
castle
phpthumb
yorick
sitemesh
chronoforms
referrer
tridion-worldserver
gcj
blackberry-playbook
genshi
visitor-statistic
w3wp.exe
blitz++
castle-monorail
swing-app-framework
eqatec
geneva-server
django-notification
ajax-forms

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