gpt4 book ai didi

python - 如何比较两个数据框列?

转载 作者:行者123 更新时间:2023-11-30 22:24:09 25 4
gpt4 key购买 nike

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().

当我比较主队和客队的得分时,错误出现在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

关于python - 如何比较两个数据框列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47901157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com