I want to work with data of NBA. That is why I have to make comparison. I need to get home win percentage. However it cannot convert string to int.
results["HomeWin"]=int(results["Home Team"])<int(results["OT?"])
y_true=results["HomeWin"].values
print("Home win percentage is{0:.1f}%".format(100*results["HomeWin"].sum()/results["HomeWin"].count()))
error is:cannot convert the series to type 'int'
Has QUIT--Anony-Mousse
77.8k14 gold badges146 silver badges198 bronze badges
1 Answer 1
You need cast by Series.astype string numbers to int:
results["HomeWin"] = results["Home Team"].astype(int) < results["OT?"].astype(int)
Sample:
import pandas as pd
results = pd.DataFrame({'Home Team':['1','2','3'],
'OT?':['4','2','1']})
print (results)
Home Team OT?
0 1 4
1 2 2
2 3 1
results["HomeWin"] = results["Home Team"].astype(int) < results["OT?"].astype(int)
print (results)
Home Team OT? HomeWin
0 1 4 True
1 2 2 False
2 3 1 False
answered Jul 26, 2016 at 8:09
jezrael
868k103 gold badges1.4k silver badges1.3k bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
fallen
it gives error as: invalid literal for long() with base 10: 'November'
jezrael
There is problem you have string data in columns like
November which cannot be converted to number. You can check this problematic values by print (results.ix[pd.to_numeric(results["Home Team"], errors='coerce').isnull() | pd.to_numeric(results["OT?"], errors='coerce').isnull(), ['Home Team','OT?']])jezrael
You can check it by
results = pd.DataFrame({'Home Team':['1','2','3', 'November'], 'OT?':['4','September','1', '5']})lang-py
astype(int):results["HomeWin"]=results["Home Team"].astype(int)<results["OT?"].astype(int)