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

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

Creating new column with values that depends on other columns

问题

以下是您的代码的翻译部分:

  1. 这是我的数据框架
  2. import pandas as pd
  3. url = 'https://www.basketball-reference.com/boxscores/pbp/200911060GSW.html'
  4. dfs = pd.read_html(url)
  5. df = dfs[0]
  6. df.columns = df.columns.droplevel() # 删除数据框架的“1st Q”多级标题
  7. df.rename(columns={'Unnamed: 2_level_1': 'PM1', 'Unnamed: 4_level_1': 'PM2'}, inplace=True)
  8. 然后我对库里的数据进行了子集化因为我关注他的动作
  9. df_curry = df.loc[df["Golden State"].str.contains("Curry", na=False)]
  10. df_curry`
  11. 现在我尝试将投篮命中和未命中的投篮添加到一个新列中以便稍后计算命中率但我总是得到错误消息str' object has no attribute 'str'”。也许有人可以帮助我或给我另一种方法。
  12. # 计算命中率
  13. field_throws_missed = 0
  14. field_throws_hit = 0`
  15. # 创建新列
  16. df_curry["Field Goals Hit"] = 0
  17. df_curry["Field Goals Missed"] = 0
  18. df_curry["Field Goals Percentage"] = 0`
  19. for row in range(len(df_curry["Golden State"])):
  20. if df_curry.iloc[row]["Golden State"].str.contains("misses 2|misses 3"):
  21. field_throws_missed += 1
  22. df_curry.iloc[row]["Field Goals Missed"] = field_throws_missed
  23. elif df_curry.iloc[row]["Golden State"].str.contains("makes 2|makes 3"):
  24. field_throws_hit += 1
  25. df_curry.iloc[row]["Field Goals Hit"] = field_throws_hit`

希望这能帮助您理解您的代码。如果您有其他问题,请随时提出。

英文:

This is my dataframe

  1. import pandas as pd
  2. url = 'https://www.basketball-reference.com/boxscores/pbp/200911060GSW.html'
  3. dfs = pd.read_html(url)
  4. df = dfs[0]
  5. df.columns = df.columns.droplevel() # drops the "1st Q" Multilevel header of the dataframe
  6. 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.

  1. df_curry = df.loc[df["Golden State"].str.contains("Curry", na=False)]
  2. 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

  1. # Calculating Hit Rate
  2. field_throws_missed = 0
  3. field_throws_hit = 0`
  4. # Creating the new Columns
  5. df_curry["Field Goals Hit"] = 0
  6. df_curry["Field Goals Missed"] = 0
  7. df_curry["Field Goals Percentage"] = 0`
  8. for row in range(len(df_curry["Golden State"])):
  9. if df_curry.iloc[row]["Golden State"].str.contains("misses 2|misses 3"):
  10. field_throws_missed += 1
  11. df_curry.iloc[row]["Field Goals Missed"] = field_throws_missed
  12. elif df_curry.iloc[row]["Golden State"].str.contains("makes 2|makes 3"):
  13. field_throws_hit += 1
  14. df_curry.iloc[row]["Field Goals Hit"] = field_throws_hit`

答案1

得分: 0

不需要循环,要计算True值的数量,请使用Series.cumsum进行累积求和:

  1. df_curry = df.loc[df["Golden State"].str.contains("Curry", na=False)].copy()
  2. df_curry["Field Goals Hit"] = df_curry["Golden State"].str.contains("misses 2|misses 3").cumsum()
  3. df_curry["Field Goals Missed"] = df_curry["Golden State"].str.contains("makes 2|makes 3").cumsum()

编辑:如果需要在下一行添加1,请使用以下代码:

  1. df_curry["Field Goals Hit"] = df_curry["Golden State"].str.contains("misses 2|misses 3").shift(fill_value=0).cumsum()
  2. 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:

  1. df_curry = df.loc[df["Golden State"].str.contains("Curry", na=False)].copy()
  2. df_curry["Field Goals Hit"] = df_curry["Golden State"].str.contains("misses 2|misses 3").cumsum()
  3. 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:

  1. df_curry["Field Goals Hit"] = df_curry["Golden State"].str.contains("misses 2|misses 3").shift(fill_value=0).cumsum()
  2. 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:

确定