第一次嘗試:
Players_data['2017_Win'] = Players_data['2017_Win'].str.replace('%','',regex=False)
for i in range(len(Players_data['2017_Win'])):
float(Players_data['2017_Win'][i])報錯:ValueError: could not convert string to float: ''
第二次嘗試:
Players_data['2017_Win'] = Players_data['2017_Win'].apply(lambda x: np.nan if x in ['-'] else x[:-1]).astype(float)/100
仍然報錯:ValueError: could not convert string to float: ''
想請教一下:為什么''內沒有任何內容也沒有空格為什么還是不能由字串轉為浮點數??
感謝!
uj5u.com熱心網友回復:
第三次嘗試:def convert_percent(value):
new_value = value.replace('%','')
new_value = new_value.strip()
return float(new_value) / 100
Players_data['2017_Win'].apply(convert_percent)
仍然一樣地報錯......
uj5u.com熱心網友回復:
解決了,查看源檔案發現是前面把缺失值給清空了,導致有一個索引不到轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/235750.html
