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

Pandas Unmelt dataset
multicollinearity for one-hot encoding
How to work with 'NA' in pandas?
Pandas DataFrame.assign arguments
zipline error KeyError: <type 'zipline.assets._assets.Equity'>
How can i split 'Date' column into 'Date' and 'Time' column? [closed]
Pandas customized group aggregation
What is the significance of t-stats value while applying ttest_ind on two pandas series?
Align order of multiindex dataframe with the order of another multiindex df, pandas
From Object To Date in Pandas [duplicate]
How do I configure PyCharm to Pandas
cannot convert nan to int (but there are no nans)
2 dimension time series groupby in pandas
display the rows based on the last occurrence of an element in a column in Pandas dataframe
How to clip pandas dataframe in place
Pandas get from pandas.core.frame.Pandas object

Categories

HOME
twitter
multithreading
spring-cloud-stream
enterprise-library-5
constraint-programming
user-input
spring-xd
numeral.js
moonmail
ups
lombok
facebook-page
text-rendering
google-cloud-speech
lucene.net
reverse-proxy
kryo
autoconf
intentfilter
kvc
realex-payments-api
claims-based-identity
lcd
hammerspoon
url-scheme
wpfdatagrid
fluentvalidation
jspm
gitignore
google-cloud-nl
tasklet
fabric8
frame
oracle-fusion-middleware
contact-form
column-family
framemaker
android-kernel
mozilla
mapbox-gl
html5-fullscreen
x11-forwarding
espeak
libvpx
hue
leading-zero
broadcastreceiver
preconditions
galen
google-closure
spring-security-kerberos
apple-news
taffy
dds
powercli
veracode
git-diff
kbuild
windows-iot-core-10
pdfclown
minimization
query-performance
quartz-composer
eventkit
color-picker
merge-conflict-resolution
xml-attribute
sigabrt
php-parse-error
asp.net5
teamcity-8.0
varargs
embedded-code
operation
processmodel
iiviewdeckcontroller
clipperlib
nsmutabledictionary
system.reflection
python-green
block-device
clicktag
markers
preferences
valuechangelistener
seaside
mechanize-ruby
access-rights
gridfs
coverflow
yui-compressor
sqlperformance
factory-method
referrer
anonymous-methods
objective-c-2.0
netbeans-6.9
coredump
asp.net-profiles
firefox4
audio-capture
avatar

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