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

converting a dictionary with with multi values for each key to dataframe
Faceted plots of a multi-indexed DataFrame
How can I select rows from one DataFrame, where a part of the row's index is in another DataFrame's index and meets certain criteria?
How can I find correlation between tags with Pandas?
using time zone in pandas to_datetime
How to replace items with their indices in a pandas series
Check number of unique values in pandas dataframe
Finding the time spent by id in each location
dropping various columns using iloc
pandas Selecting/sampling at different interval frequencies
First five non-numeric, non-null, distinct values from a column
How to operate conditional calculation between columns in pandas dataframe?
Group by groups to Pandas Series/Dataframe
How to write a multiple dataframes to same sheet without duplicating the column labels
logic element-wise operations in pandas time-series dataframe
Load csv data to spark dataframes using pd.read_csv?

Categories

HOME
hive
mediawiki
angular-material
session
tesseract
infragistics
amazon-ecs
umd
retrofit
windows-server
ezpublish
upload
qore
synchronization
medical
export-to-csv
apache-cayenne
alignment
karma-jasmine
textfield
zapier
tokenize
extjs5
chromium-embedded
zurb-foundation-6
java-7
c++-amp
google-cloud-nl
procdump
librato
socialengine
bcd
google-cloud-endpoints-v2
exuberant-ctags
noraui
phpfreechat
atl
mapbox-gl
vxworks
restlet
scaffold
event-driven
angularjs-factory
tasker
ensembles
slick-3.0
termination
revapi
errordocument
fancybox-2
mplayer
elgg
nativeapplication
csound
nbconvert
smart-table
pintos
abcpdf9
websitepanel
pagedlist
ipconfig
xna-4.0
reactive-banana
endeca-workbench
comobject
varargs
browser-bugs
arcanist
sailfish-os
nsight
proj4js
block-device
clicktag
tablelayout
fpml
websocket4net
neolane
onactivityresult
google-reader
ceil
heisenbug
p4java
kgdb
srs
excel-2003
random-seed
enterprisedb
simba
mhtml
dmoz
coderush
propertyeditor
pysimplesoap
netbeans-6.9
amazon-appstore
nsobject
zpt
tomcat-valve
appendto
yui-datatable
lang
paster
sortable-tables
web-application-design
modelstate
routedevent
perfect-hash
ncqrs
mediarss
uiq3

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile