0

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'

1
  • 1
    use astype(int) : results["HomeWin"]=results["Home Team"].astype(int)<results["OT?"].astype(int) Commented Jul 26, 2016 at 8:09

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

it gives error as: invalid literal for long() with base 10: 'November'
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?']])
You can check it by results = pd.DataFrame({'Home Team':['1','2','3', 'November'], 'OT?':['4','September','1', '5']})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.