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 properly sample from a numpy.random.multivariate_normal (positive-semidefinite covariance matrix issue)
how to do logical operation between dataframe columns?
Console hangs up at the time of plotting
Pandas apply a function at fixed interval
float type column in pandas to convert to tuple/list
Getting an error with Pandas Panel boolean indexing
pandas dataframe subtraction causing nan
Pandas dataframe: truncate string fields
how to add new categorical column in pandas
Finding different Ids with the same value in pandas dataframe
Why can't iterrows do math - and instead returns integer values where these should be floats
How to merge/concatenate based on column multiindex? (Pandas)
Groupby function on pandas dataframe
How are the nan values filled in x.add(y, fill_value = 0)?
mulitiindexing in python: how to select level0-index based on multiple values in level1 rows
What's the Pandas way to write `if()` conditional between two `timeseries` columns?

Categories

HOME
maven
wso2-am
proxy
tizen
objectgears
xmpp
framework7
at-command
analysis
malloc
jxls
webpack-2
google-project-tango
installshield
ojdbc
onelogin
django-imagekit
constraint-programming
android-youtube-api
medical
facebook-php-sdk
modx-revolution
graphlab
ef-migrations
django-cms
introduction
jtextfield
predix
custom-wordpress-pages
errorlevel
accessor
excel-2007
trading
centos6.5
wpfdatagrid
delicious-api
instant-messaging
stormpath
io-redirection
retina-display
quadratic-programming
botbuilder
eigenvalue
node-sass
.net-4.6.2
apple-news
auto-update
rails-routing
logparser
blogengine.net
veracode
rdfs
libusb-win32
pearson
iso8601
chord-diagram
crosswalk-runtime
xcb
gridview-sorting
websitepanel
pagerank
ptrace
unity-networking
spim
reactive-banana
census
notify
codeigniter-url
android-nested-fragment
javafx-webengine
titanium-modules
zend-route
responsive-slides
uitouch
marmalade
terminal-services
c18
apc
dataadapter
qt-faststart
padarn
interface-orientation
android-hardware
free-variable
isnullorempty
nsnetservice
coderush
viewswitcher
hgsubversion
heartbeat
cinema-4d
servicehost
mysql-error-1005
cxxtest
msdev
grid-system

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