我正在尝试将数据框中的所有数字转换为浮点数。到目前为止,我已经取得了成功,除了百分之一列。我得到错误'浮动的无效文字:0.00%'但它似乎适用于其他一切。到目前为止,这是我的代码:
def clean():
rawdata = concat()
rawdata = rawdata[['Channel','Advertiser Domain','Bids','Imp.','Avg. Bid CPM (USD)','Revenue (USD)','eCPM (USD)','Wins','Date']]
rawdata[['Bids','Imp.','Avg. Bid CPM (USD)','Revenue (USD)','eCPM (USD)','Wins']] \
= rawdata[['Bids','Imp.','Avg. Bid CPM (USD)','Revenue (USD)','eCPM (USD)','Wins']].\
replace([',','%'],'').astype(float)
return rawdata
解决了:
def clean():
rawdata = concat()
rawdata = rawdata[['Channel','Advertiser Domain','Bids','Imp.','Avg. Bid CPM (USD)','Revenue (USD)','eCPM (USD)','Wins','Date']]
for col in ['Bids','Imp.','Wins']:
rawdata[col] = rawdata[col].str.replace(',','').str.replace('%','')
for com in ['Bids','Imp.','Avg. Bid CPM (USD)','Revenue (USD)','eCPM (USD)','Wins']:
rawdata[com] = rawdata[com].astype(float)
return rawdata.sort('Revenue (USD)', ascending=False)
答案 0 :(得分:1)
修改您的replace
以包含regex=True
:
rawdata = rawdata.replace([',','%'],'', regex=True).astype('float')