分组依据和条件

问题描述 投票:0回答:1

我想分类

名称 班级 总计 成本成本
生物技术发现 A 1.82% 0.75%
生物技术发现 C 2.40% 1.00%
生物技术发现 N 2.57% 1.25%
生物技术发现 1.15% 0.00%
全球增长 A 1.89% 0.75%
全球增长 N 2.64% 1.25%
全球增长 1.15% 0.00%

每个“NOMBRE”内

如果成本等于 0 机构

如果成本最低 Barata

如果成本 > 分钟卡拉

如果成本是最大超级卡拉

名称 班级 总计 成本成本
生物技术发现 A 1.82% 0.75%
生物技术发现 C 2.40% 1.00%
生物技术发现 N 2.57% 1.25%
生物技术发现 1.15% 0.00%
全球增长 A 1.89% 0.75%
全球增长 N 2.64% 1.25%
全球增长 1.15% 0.00%
pandas dataframe if-statement conditional-statements where-clause
1个回答
0
投票

您可以通过一系列直接执行过滤的步骤来完成此操作。首先,将标签初始化为“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
© www.soinside.com 2019 - 2024. All rights reserved.