您可以通过一系列直接执行过滤的步骤来完成此操作。首先,将标签初始化为“cara”。这代表最小值和最大值之间的所有值。从那里,我们只需要找到您的零、最小值和最大值。
# Convert percent to a number
df['COST num'] = df['COST Cost'].str.slice(0,4).astype('float64')
# Initialize the label to 'cara.' This represents all the in-between values.
df['label'] = 'cara'
# Set 0 to INSTITUCIONAL
df.loc[df['COST num'] == 0, 'label'] = 'INSTITUCIONAL'
# Get min value where cost is not 0
df.loc[df[df['COST num'] > 0].groupby('Nombre')['COST num'].idxmin(), 'label'] = 'barata'
# Get max value
df.loc[df.groupby('Nombre')['COST num'].idxmax(), 'label'] = 'super cara'
# Remove the converted value
df = df.drop('COST num', axis=1)
Nombre Class TOTAL COST Cost label
0 Biotechnology Discovery A 1.82% 0.75% barata
1 Biotechnology Discovery C 2.40% 1.00% cara
2 Biotechnology Discovery N 2.57% 1.25% super cara
3 Biotechnology Discovery I 1.15% 0.00% INSTITUCIONAL
4 Global Growth A 1.89% 0.75% barata
5 Global Growth N 2.64% 1.25% super cara
6 Global Growth I 1.15% 0.00% INSTITUCIONAL