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 subtract one dataframe from another?
Pandas Inter-row calculations
how to make pandas HDFStore 'put' operation faster
Handling detection limits in a Series
Change Categorical Variable levels to What I provide/Combine levels two categorical variables
pandas: read_csv combined date-time columns as index into a dataframe
apply on group replicating complete MultiIndex
Convert csv file to pandas dataframe
Get unique values from index column in MultiIndex
Pandas dataframe resample at every nth row
how to calculate the differences of a list of pandas timestamps?
Escaped quotes in pandas read_csv
Resample Series/DataFrame with frequency anchored to specific time
Error when calling R from Pandas
Merge of multiple data frames of different number of columns into one big data frame
Count by unique pair of columns in pandas

Categories

HOME
compiler-construction
yii2
wso2-am
hive
pypi
oracle11g
pycharm
vmware
relay
cplex
routes
getelementsbytagname
infragistics
ezpublish
qore
virtualization
decimal
carthage
try-catch
serverless-framework
clearcase-ucm
seaborn
serilog
pythonanywhere
flux
textfield
immutable.js
nas
cx-freeze
sylius
selectedindexchanged
traffic
uninstall
fifo
assistant
.net-4.0
uisplitview
data-manipulation
ioio
create-table
google-qpx-express-api
accelerate-framework
amazon-kinesis-kpl
environment-modules
catalog
suricata
code-search-engine
trim
broadcastreceiver
janrain
fakeiteasy
avconv
rainbowtable
filepicker
errordocument
logparser
python-webbrowser
alphabet
winscp-net
mplayer
tactic
google-perftools
knpmenubundle
arrow-keys
sonarlint-vs
smart-table
root-framework
angular-strap
abcpdf9
wdf
spring-android
storekit
multiple-regression
fputcsv
angstrom-linux
markojs
php-internals
system.management
phpcas
hsv
jmeter-maven-plugin
tarjans-algorithm
android-listview
key-management
jscript.net
qpainter
cdt
responsive-slides
dealloc
resty-gwt
c18
gridfs
batterylevel
android-hardware
clipper
browser-detection
chuck
dsn
parametric-equations
nsmanagedobject
custom-backend
eclipse-templates
pydot
table-footer
asp.net-mvc-areas
filtered-index
database-management
javap
virtual-functions
commodore
misv





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