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

Add column with number of days between dates in DataFrame pandas
Grouped function between 2 columns in a pandas.DataFrame?
Pandas dataframe row removal
pandas in pycharm with too many columns
Selection in Multi-Indexed DataFrame when key does not exists
basic math function max() help in pandas or numpy redux: for a panel [duplicate]
Problems with pandas tutorial - missing file bikes.csv and special characters in keys
Best way to Shift along a Multi Index Time series
Frequency distribution of series in pandas
Calculate First Date for Product Lifecycle Analysis with pandas
What are the types that pandas.read_csv might infer?
Apply to each element in a Pandas dataframe
Pandas Series .loc() access error after appending
Pandas Grouping Select entire Column
Where did the Dataframe summary view gone on pandas 0.13?
Pandas dataframe operations

Categories

HOME
jdo
bluetooth
cloud
vbscript
ngrx
layout
relay
malloc
rubygems
win32gui
node-notifier
basic
gitpitch
postgres-xl
fortumo
uitypeeditor
workload-scheduler
worldwind
solaris-10
evopdf
dbext
jasonette
graphicsmagick
accessor
zurb-foundation-6
phpfox
strncpy
replaceall
ejabberd-module
nouislider
bcd
noraui
code-contracts
azure-sql-database
mapdb
stacked
event-driven
document.write
dynamic-reports
password-encryption
mime
unixodbc
logfiles
logparser
python-webbrowser
elgg
darcs
slickedit
tactic
nodebb
feeds
pdfclown
flow-control
color-profile
orthogonal
yt-project
cubes
wdf
android-fonts
heidisql
passport-google-oauth
url-masking
adxstudio-portals
transmitfile
ctest
firebaseui
ipconfig
independentsoft
intel-fortran
sdhc
intellij-14
atk4
feedback
citrus-pay
census
remobjects
device-orientation
fscommand
gui-test-framework
htmlcleaner
ember-charts
industrial
quantlib-swig
eclipse-memory-analyzer
seed
frameset
newtonscript
objective-c-2.0
osql
vdsp
coercion
lang
radcombobox
cxxtest
mirah
sector
private-members
w3wp
document-conversion

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App