我想根据 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'
下面是输出:
有人能帮帮我吗?
1条答案
按热度按时间ia2d9nvy1#
我会使用
pandas.cut
来保存时间、能源和内存:#输出: