pandas 函数2根据列值为类别赋值不起作用,pd Dataframe

7gyucuyw  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(131)

我想根据 Dataframe 中特定列的值为每一行分配一个类别。函数如下:

def assign_SOP (df):
  if df['Strikeouts per Pitches'] <= 0.053571:  
    return 'Below average'
  elif df['Strikeouts per Pitches'] >= 0.053571 and df['Strikes per Pitches'] < 0.059794:
    return 'Average'
  elif df['Strikeouts per Pitches'] >= 0.059794 and df['Strikes per Pitches'] < 0.068870:
    return 'Above Average'
  elif df['Strikeouts per Pitches'] >= 0.068870:
    return 'Elite'
#Creating Columsn for each category
df_MLB['SP Category'] = df_MLB.apply(assign_SP, axis=1)
df_MLB['SOP Category'] = df_MLB.apply(assign_SOP, axis=1)

不知何故,它只对“低于平均水平”和“精英”有效

我在另一个专栏中使用了几乎相同的函数,它起作用了:

def assign_SP (df):
  if df['Strikes per Pitches'] <= 0.645129:
    return 'Below average'
  elif df['Strikes per Pitches'] >= 0.645129 and df['Strikes per Pitches'] < 0.656995:
    return 'Average'
  elif df['Strikes per Pitches'] >= 0.656995 and df['Strikes per Pitches'] < 0.672696:
    return 'Above Average'
  elif df['Strikes per Pitches'] >= 0.672696:
    return 'Elite'

下面是输出:

有人能帮帮我吗?

ia2d9nvy

ia2d9nvy1#

我会使用pandas.cut来保存时间、能源和内存:

import numpy as np

categories = ['Below average', 'Average', 'Above Average', 'Elite']
​
values = [0, 0.053571, 0.059794, 0.068870, np.inf]
​
df["SOP Category"] = pd.cut(df["Strikeouts per Pitches"], bins=values, labels=categories, include_lowest=True)
​
#输出:
print(df)
    Strikeouts per Pitches   SOP Category
0                 0.064281  Above Average
1                 0.054225        Average
2                 0.064516  Above Average
3                 0.063732  Above Average
4                 0.060326  Above Average
5                 0.056730        Average
6                 0.078766          Elite
7                 0.068870  Above Average
8                 0.058195        Average
9                 0.052836  Below average
10                0.050294  Below average
11                0.057866        Average
12                0.074221          Elite
13                0.059794        Average
14                0.052574  Below average
15                0.045643  Below average
16                0.048541  Below average
17                0.065417  Above Average
18                0.064903  Above Average
19                0.077328          Elite
  • 注意:你必须为每一列分别做一个切口。*

相关问题