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

plotting histograms in pandas
How to map values in a list to a pandas dataframe with binary values
loosing rows in pandas dataframe after apply method that doesnt delete row
Memory is not released when taking a small slice of a DataFrame
How to pandas groupby and pivot_table to have pivto tables look like Excel's
Pandas: Series of quotients of adjacent elements
Zodb process Killed after reading multiple fs
pandas group By select columns
Pandas fill cells in a column with NaN values, derive the value from other cells in the row
Merge files based on a date range?
pandas pivot_table: values per column instead of columns per value
NaNs disappear after saving to excel file
Is there any way to convert columns's value from nonconsecutive numbers to 1-started consecutive numbers?
pandas astype categories not working
Scikit-learn and pandas in Sage 7.2
Does Pandas calculate ewm wrong?

Categories

HOME
pandas
openmp
keycloak
pycharm
session
syntax
electron
q
yarn
jgroups
sql-server-2016
disassembler
opengl-es-2.0
mouse
autotools
cloudkit
node-pdfkit
mapserver
google-cloud-ml
circular-dependency
fallback
zebra-printers
caml
systemc
finite-automata
errorlevel
underflow
spark-jobserver
poltergeist
rst2pdf
jaxb2
windows-server-2000
primitive
microsoft-chart-controls
tinymce-4
bosh
oracle-fusion-middleware
karaf
accelerate-framework
cloud-code
file-format
greenrobot-eventbus
google-closure
outlook-api
slick-3.0
convertapi
flickr-api
dotcover
komodoedit
paxos
boost-multi-index
dotnetzip
google-perftools
windows-iot-core-10
sonarlint-vs
chord-diagram
synchronous
quartz-composer
migradoc
angular-strap
xcb
httplistener
merge-conflict-resolution
clang-static-analyzer
moveit
qcustomplot
pagerank
nessus
xpath-1.0
javax.sound.midi
ios8-today-widget
srand
execute
eclipse-clp
mutation-observers
wireshark-dissector
spidermonkey
mono-embedding
clipperlib
bridge.net
titanium-modules
page-layout
runtime.exec
web2py-modules
dataservice
android-hardware
wsdl-2.0
angularjs-controller
path-separator
frameset
postgresql-performance
subgurim-maps
yui-datatable
mirah
rescale
xetex
privilege

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