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: map color argument by multidict
Convert categorical variables from String to int representation
using matplotlib colormap with pandas dataframe.plot function
Why head() function on a panda's dataframe displays nothing?
Convert pandas to dictionary defining the columns used fo the key values
Understanding pandas interpolation function
How can I select the indexes where my dataframe has more than two entries?
Pandas HDFS Unicode Issue
Tobin's Annualized Standard Deviation in Pandas
How do you read a geojason url into a geopandas dataframe or pandas dataframe?
Add column index to existing pandas dataframe
IPython : groupby column to find processing time
How to get the value in front of a specified value from Series
Extract specific columns from a given webpage
Pandas select data in q quantile
Pandas: How to apply a groupby with as_index=False

Categories

HOME
deployment
hook
pycharm
push-notification
homebrew
iterator
hashmap
getelementsbytagname
jira
networkx
qore
kibana-4
windows-10-universal
vifm
timeout
vaadin7
workload-scheduler
tostring
ef-migrations
serilog
custom-wordpress-pages
kudan
emgucv
maxmind
elasticsearch-net
kendo-datasource
yadcf
google-sites-2016
winrt-xaml-toolkit
grails-3.1
webtest
xmlreader
fusionpbx
disassembling
libraries
vapor
mesos-chronos
ws-security
gulp-sourcemaps
http-live-streaming
jvm-languages
isbn
executenonquery
auto-update
smartcontracts
python-webbrowser
rdfs
integrity
idisposable
git-diff
spring-mongodb
django-scheduler
chord-diagram
migradoc
google-cdn
time-and-attendance
deadbolt-2
instant
azure-sdk
url-masking
intrusion-detection
bgp
ios4
photobucket
energy
spim
xna-4.0
lemon
sdhc
thredds
coveralls
apache-commons-fileupload
census
dundas
p2
key-management
formatjs
block-device
codeigniter-url
client-side-templating
responsive-slides
has-many-through
dml
picturefill
mysqltuner
listings
unc
cufon
whoosh
php-parser
window-management
appender
android-sdk-2.1
datareader
telerik-scheduler
yslow
sortable-tables
suppress
jquery-ui-droppable
exchange-server-2003
swing-app-framework
avatar
scripting-languages
misv

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile