创建一个新列,其值取决于其他列。

huangapple go评论82阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年1月9日 17:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定