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 create a lagged data structure using pandas dataframe
pandas dataframe interpolate
pandas dataframe interpolating missing days
procedurally convert interval data to cross sectional data
Pandas Resample Strange Zero Tolerance Behavior
VLOOKUP equivalent function to look up value in pandas DataFrame
pandas dataframe shift dates
ipython dataframe plotting setting color parameter?
How to change string columns size for all columns in pandas hdfstore?
is there an equivalent of data-frame in OCaml?
Pandas option to keep levels after xs operation
Pandas DataFrame Column rename error… am I'm being silly?
Assigning one column to another column between pandas DataFrames (like vector to vector assignment)
Incompatible indexer with Series
data wrangling with Flask: how to do this using SQL language? Does it make sense to use pandas?
Update columns in dataframe inside panel without for loop?

Categories

HOME
winforms
azure-data-factory
angular-material
stock
iot
octobercms
fingerprint
opengl-es-2.0
indesign
portia
metatrader4
node-pdfkit
resize
collectd
hex-editors
decimal
caml
clojurescript
pythonanywhere
alpine
visjs
riot.js
kvc
format-specifiers
pepper
typo3-6.2.x
c++-amp
unboundid
socialengine
yadcf
phonegap
exuberant-ctags
scorm2004
form-data
mpmediaquery
git-merge
xcode-extension
automake
service-discovery
cookiecutter-django
tasker
http-live-streaming
ibpy
skeleton-css-boilerplate
auto-update
smartcontracts
azure-application-gateway
ionicons
epson
sqldf
vtigercrm
query-performance
or-tools
migradoc
objective-c-swift-bridge
jquery-filter
ado.net-entity-data-model
plottable.js
player
storekit
hill-climbing
qcustomplot
superstack
testng-dataprovider
asp.net5
uid
unity-networking
vhd
cartesian-product
libressl
embedded-code
drawbitmap
arcanist
sysinternals
ami
sonarqube5.1.2
geonetwork
dd
phalanger
device-manager
markers
app42
mbr
dataservice
padarn
hungarian-algorithm
spring-validator
tinn-r
propertyeditor
cascalog
armcc
php-parser
cinema-4d
floating
multi-tier
iweb
kdbg
virtual-functions
dbisam
django-notification
lzh
ntvdm.exe

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