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 do I select where exists in pandas?
Compute rowmeans ignoring na in pandas, like na.rm in R
Pandas apply to all values except missing
How to bind s parameter of scatter function in matplotlib to number of occurences of coordinates in pandas dataframe?
Creating a Dropdown menu in Plotly from Pandas
Pandas timestamp on array
create pandas dataframe from list of tuples
Turn values in a DataFrame column into column labels
Pandas & AWS Lambda
Pandas Year-Month format to timestamp
group by time and other column in pandas
Pandas `apply` into multiple columns consuming lots of memory
How to plot aggregated DataFrame using two columns?
Built-in support for converting column names into values?
Spark: LinearRegressionWithSGD and Pandas OLS do not give same results
convert yyyy.mm.dd time.stamp to yyyy.mm time stamp in pandas python

Categories

HOME
openstack
deezer
electron
google-docs
adb
fsm
alpha
ezpublish
azure-storage-tables
kibana-4
midi
offline
spring-tool-suite
usergrid
fallback
quartz-scheduler
lombok
reactive-cocoa
pythonanywhere
django-cms
google-pagespeed
jtextfield
autocad-plugin
restful-authentication
icloud-api
poltergeist
internet-explorer-9
maquette
gsoap
lxd
windows-dev-center
form-data
ios5
simplexml
event-driven
abstract-class
reportbuilder
unoconv
midl
rainbowtable
veracode
nstextview
darcs
typed-lambda-calculus
ifs
mako
flow-control
url-masking
dstu2-fhir
file-writing
fody
fuzzy-search
skype4py
eclipse-clp
marching-cubes
wireshark-dissector
coveralls
browser-bugs
iiviewdeckcontroller
lustre
zend-route
project-planning
mesa
p4java
mysql-error-1062
undefined-reference
bundles
resty-gwt
ril
html4
frameset
online-compilation
infobox
servicehost
ecl
yui-datatable
nsviewanimation
vc90
nerddinner
data-acquisition

Resources

Encrypt Message