[英]How to compare two dataframes columns?
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:Series的真值是不明確的。 使用a.empty,a.bool(),a.item(),a.any()或a.all()。
當我比較主隊和客隊的得分時,錯誤處於if條件。我想要做的是在csv文件中添加一個列,列出團隊的勝負,勝利為1,虧損為零這樣我就可以在一個賽季中加入特定球隊的勝利並計算他們獲勝的概率並預測下一賽季的勝利概率,
你可以這樣做:
df_2010['Win'] = df_2010['Home Score'] > df_2010['Away Score']
您不需要切片數據框。
這是一個完整的例子:
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)
哪個會增加
Home Score Away Score
0 3 3
1 4 2
2 4 1
3 4 4
4 4 2
一個額外的列win
像這樣:
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
我認為您可以通過比較列創建布爾掩碼,然后分配新列:
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
它與:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.