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

rolling polynomial regression in pandas
python list to dataframe object
Find string in multiple columns ?
Drop level from one specific column
build sums of columns of pandas dataframe despite missing some data
Index column names
convert hourly time period in 15-minute time period
Process an entire column from a DataFrameGroupby
How to create new column with positive instead negative values
Pandas: Location of a row with error
How to create new Pandas Dataframe with columns form DataFrame (PYTHON)
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

Categories

HOME
vim
coq
debugging
proxy
zeromq
iterator
q
paw-app
youtube-api-v3
portia
webrequest
wheelnav.js
dax
usergrid
messages
decomposition
dosgi
saxon
undefined
google-pagespeed
predix
grails3
one-hot-encoding
tar
accessor
hammerspoon
emgucv
url-scheme
bootstrap-duallistbox
nhapi
linkerd
minitab
r-raster
directx-10
oracle-fusion-middleware
pdb
pim
normal-distribution
mapdb
html5-fullscreen
overriding
neuroscience
mapzen
arena-simulation
businessworks
convertapi
termination
clean-architecture
sage-one
dropbox-php
boost-preprocessor
synchronous
or-tools
yt-project
bonobo
disque
markojs
ios4
netmq
rtbkit
jqgrid-formatter
muse
deis
gwidgets
java.util.concurrent
neolane
ceil
xceed-datagrid
dealloc
jboss-weld
didselectrowatindexpath
.aspxauth
cdc
gridfs
enterprisedb
robotics-studio
gil
twrequest
tridion-worldserver
online-compilation
nsmanagedobject
hgsubversion
zpt
oncheckedchanged
mozilla-prism
ext3
meego

Resources

Encrypt Message