英文:
Creating new column with values that depends on other columns
问题
以下是您的代码的翻译部分:
这是我的数据框架
import pandas as pd
url = 'https://www.basketball-reference.com/boxscores/pbp/200911060GSW.html'
dfs = pd.read_html(url)
df = dfs[0]
df.columns = df.columns.droplevel() # 删除数据框架的“1st Q”多级标题
df.rename(columns={'Unnamed: 2_level_1': 'PM1', 'Unnamed: 4_level_1': 'PM2'}, inplace=True)
然后,我对库里的数据进行了子集化,因为我关注他的动作。
df_curry = df.loc[df["Golden State"].str.contains("Curry", na=False)]
df_curry`
现在我尝试将投篮命中和未命中的投篮添加到一个新列中,以便稍后计算命中率,但我总是得到错误消息“str' object has no attribute 'str'”。也许有人可以帮助我或给我另一种方法。
# 计算命中率
field_throws_missed = 0
field_throws_hit = 0`
# 创建新列
df_curry["Field Goals Hit"] = 0
df_curry["Field Goals Missed"] = 0
df_curry["Field Goals Percentage"] = 0`
for row in range(len(df_curry["Golden State"])):
if df_curry.iloc[row]["Golden State"].str.contains("misses 2|misses 3"):
field_throws_missed += 1
df_curry.iloc[row]["Field Goals Missed"] = field_throws_missed
elif df_curry.iloc[row]["Golden State"].str.contains("makes 2|makes 3"):
field_throws_hit += 1
df_curry.iloc[row]["Field Goals Hit"] = field_throws_hit`
希望这能帮助您理解您的代码。如果您有其他问题,请随时提出。
英文:
This is my dataframe
import pandas as pd
url = 'https://www.basketball-reference.com/boxscores/pbp/200911060GSW.html'
dfs = pd.read_html(url)
df = dfs[0]
df.columns = df.columns.droplevel() # drops the "1st Q" Multilevel header of the dataframe
df.rename(columns={'Unnamed: 2_level_1': 'PM1', 'Unnamed: 4_level_1': 'PM2'}, inplace=True)
then i have made a subset of curry because I focus on his actions.
df_curry = df.loc[df["Golden State"].str.contains("Curry", na=False)]
df_curry`
now i tried to insert the hit and not hit throws into a new column to calculate the quote later but i always get the error "str' object has no attribute 'str'.
Maybe someone can help me or give me another approach
# Calculating Hit Rate
field_throws_missed = 0
field_throws_hit = 0`
# Creating the new Columns
df_curry["Field Goals Hit"] = 0
df_curry["Field Goals Missed"] = 0
df_curry["Field Goals Percentage"] = 0`
for row in range(len(df_curry["Golden State"])):
if df_curry.iloc[row]["Golden State"].str.contains("misses 2|misses 3"):
field_throws_missed += 1
df_curry.iloc[row]["Field Goals Missed"] = field_throws_missed
elif df_curry.iloc[row]["Golden State"].str.contains("makes 2|makes 3"):
field_throws_hit += 1
df_curry.iloc[row]["Field Goals Hit"] = field_throws_hit`
答案1
得分: 0
不需要循环,要计算True
值的数量,请使用Series.cumsum
进行累积求和:
df_curry = df.loc[df["Golden State"].str.contains("Curry", na=False)].copy()
df_curry["Field Goals Hit"] = df_curry["Golden State"].str.contains("misses 2|misses 3").cumsum()
df_curry["Field Goals Missed"] = df_curry["Golden State"].str.contains("makes 2|makes 3").cumsum()
编辑:如果需要在下一行添加1
,请使用以下代码:
df_curry["Field Goals Hit"] = df_curry["Golden State"].str.contains("misses 2|misses 3").shift(fill_value=0).cumsum()
df_curry["Field Goals Missed"] = df_curry["Golden State"].str.contains("makes 2|makes 3").shift(fill_value=0).cumsum()
英文:
No loops necessary here, for count True
s values use cumulative sum by Series.cumsum
:
df_curry = df.loc[df["Golden State"].str.contains("Curry", na=False)].copy()
df_curry["Field Goals Hit"] = df_curry["Golden State"].str.contains("misses 2|misses 3").cumsum()
df_curry["Field Goals Missed"] = df_curry["Golden State"].str.contains("makes 2|makes 3").cumsum()
EDIT: If need add 1
in next row use:
df_curry["Field Goals Hit"] = df_curry["Golden State"].str.contains("misses 2|misses 3").shift(fill_value=0).cumsum()
df_curry["Field Goals Missed"] = df_curry["Golden State"].str.contains("makes 2|makes 3").shift(fill_value=0).cumsum()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论