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

Change values in a column from a list
Pandas: How to Return Max Value in Multiindex
cx freeze module not correctly installed
Counting occurences from a dict and pandas
Calculating mean of a specific column by specific rows
Value counts per period, taking prior values into account
mapping values from another pandas df
Tensorflow: Cannot allocate buffer larger than kint32max for StringOutputStream
using assign and lambda to combine year and month columns into 1 date column
Apply an element-wise function on a pandas dataframe with index and column values as inputs
How to pass dataset directory in google datalab
After rename column get keyerror
pandas groupby and mean aggregation on more columns
str.replace function creating NaN data
pandas element wise conditional return index
pandas series or tidy dataframe: index level values to dataframe columns

Categories

HOME
sendgrid
testing
wso2-am
fluentd
objectgears
webstorm
adb
setup-deployment
elm
spring-jdbc
blueprintjs
acquia
portia
gorm
vifm
tomcat6
fancybox-3
nstableview
correlation
collectd
visual-studio-cordova
jsprit
undefined
jquery-ajaxq
functional-testing
tapestry
crystal-reports-2010
sqlcipher
cas
apache-commons-io
strncpy
maquette
typo3-6.2.x
buck
newline
flink-streaming
airconsole
calibre
stringtemplate
hybridauth
mozilla
google-api-nodejs-client
mmenu
suricata
stacked
automake
serverside-rendering
service-discovery
uft-api
webdriver-manager
opshub
isbn
gpx
user-accounts
filepicker
ionicons
brightcove
android-cursor
windows-mobile-6.5
qtwebview
mcafee
synchronous
google-feed-api
google-web-starter-kit
lua-5.1
ubuntu-10.04
bluegiga
eclipse-clp
feedback
ibaction
composite
xc16
codeigniter-routing
client-side-templating
android-radiobutton
marmalade
braille
picturefill
ember-charts
aqtime
soundtouch
ftps
dotnetnuke-5
bulkloader
file-locking
android-screen-support
html-editor
robotics-studio
wsdl-2.0
factory-method
unc
trusted
newtonscript
assembly-loading
pydot
deobfuscation
xmlspy
recent-documents
preference
vc90
thunderbird-lightning
photoshop-cs4
simpletest

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