import pandas as pd
import quandl
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("fivethirtyeight")
df_2010=pd.read_csv("c:/users/ashub/downloads/documents/MLB 2010.csv",index_col=0)
#print(df_2010)
sliced_data=df_2010[["Home Team","Away Team","Home Score","Away Score"]]
#print(sliced_data)
for win in sliced_data:
flag1=sliced_data["Home Team"]+str("index")
flag2=sliced_data["Away Team"]+str("index")
print(sliced_data["Home Score"],sliced_data["Away Score"])
if sliced_data["Home Score"]>sliced_data["Away Score"]:
df_2010=df_2010.join([1,0],index=[flag1,flag2])
else:
df_2010=df_2010.join([0,1],index=[flag1,flag2])
df_2010.to_html("c:/users/ashub/desktop/ashu.html")
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
The error is at if condition when i am comparing the score of home team and away team.What I want to do is to add a column to the csv file which lists the win or loss of a team,win being 1 and loss being zero so that i can add the win of a particular team in a season and calculate their probability of winning and predict the probability of winning in the next season,
You can do just this:
df_2010['Win'] = df_2010['Home Score'] > df_2010['Away Score']
You won't need that sliced data frame.
Here's a full example:
import pandas as pd
import numpy as np
df = pd.DataFrame([np.random.randint(0, 5, 5),
np.random.randint(0, 5, 5)],
index=['Home Score', 'Away Score']).T
print(df)
df['Win'] = df['Home Score'] > df['Away Score']
print(df)
Which will add to
Home Score Away Score
0 3 3
1 4 2
2 4 1
3 4 4
4 4 2
an additional column win
like this:
Home Score Away Score Win
0 3 3 False
1 4 2 True
2 4 1 True
3 4 4 False
4 4 2 True
I think you can create boolean mask by compare columns and then assign new columns:
np.random.seed(123)
sliced_data = pd.DataFrame([np.random.randint(0, 5, 5),
np.random.randint(0, 5, 5)],
index=['Home Score', 'Away Score']).T
m = sliced_data['Home Score'] > sliced_data['Away Score']
sliced_data['Away Team index'] = (~m).astype(int)
sliced_data['Home Team index'] = m.astype(int)
print(sliced_data)
Home Score Away Score Away Team index Home Team index
0 2 2 1 0
1 4 3 0 1
2 2 1 0 1
3 1 1 1 0
4 3 0 0 1
It is same as:
sliced_data['Away Team index'] = np.where(m, 0,1)
sliced_data['Home Team index'] = np.where(m, 1,0)
print(sliced_data)
Home Score Away Score Away Team index Home Team index
0 2 2 1 0
1 4 3 0 1
2 2 1 0 1
3 1 1 1 0
4 3 0 0 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.