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: merge/concat.join two dfs
Scikit learn for ranking features using RFE including scores
adding a column to Pandas dataframe based on adjacent values of existing column
Pandas merging rows with the same value and same index
I update to pandas 0.18 with conda update command. But my jupyter notebook cannot import that version and keep on using 0.15
Pandas timeseries resampling and interpolating together
assign values to a subset of rows based on a count criteria from columns in pandas
numpy array format for cross_validation
Plotting in pandas dataframe using matplotlib
Getting csv header in pandas from arbitrary row
How do I give the index column created by a functional groupby a name?
Pandas: substitute NA's with shadow values obtained from contemporary non missing values in other column
How to sort from greatest to smallest of groupby data in Pandas Python
pandas: stop pandas from auto displaying 0.40 as 0.4
How can I manage units in pandas data?
groupby .sum() returns wrong value in pandas

Categories

HOME
ms-access
cloud
openmp
json.net
sd-card
rsync
qore
adobe-analytics
reveal.js
volttron
qt-creator
event-handling
solaris-10
lldb
introduction
emulator
predix
grails3
reverse-proxy
cloudhub
accessor
icloud-api
cas
user-interaction
info.plist
objectanimator
madlib
http-status-code-503
stringtemplate
avcapturesession
stacked
restlet
adobe-premiere
xcode-extension
objectlistview
sas-visual-analytics
optix
sfdc
dynamic-reports
user-accounts
fault
python-c-api
celery-task
yii2-extension
elgg
flow-control
mcafee
lync-client-sdk
quartz-composer
background-service
probability-density
skype4py
yaws
oauth2client
associative-array
vhd
intellij-14
fouc
sankey-diagram
vstest.console.exe
notify
winddk
websocket4net
php-5.4
doskey
oracle-warehouse-builder
geos
commoncrypto
access-rights
cos
plasma
trusted
hosts-file
online-compilation
deobfuscation
psi
hamachi
appender
table-footer
recent-documents
w3wp.exe
subviews
ctp4
boost-smart-ptr
premature-optimization
.net-1.0
document-conversion

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile