我是Python的新手。请帮助我解决以下问题。
我有一个下面给出的数据集(样本数据),我想创建一个新的列,称为薪金范围。请提出如何在数据框上遍历else语句以创建新列“ Salary_range”的问题
DataFrame:
Name Salary Salary_Range
A 10 Low
B 8 Low
C 21 Medium
D 22 Medium
A 23 Medium
R 25 Medium
K 31 High
A 33 High
L 34 High
M 41 Very_High
N 44 Very_High
答案 0 :(得分:0)
df.loc 创建一个新列(如果不存在)。我想您想要这样的东西:
filter = (df['Salary'] > 35)
df.loc[filter, 'New_Salary_Range'] = 'VERY HIGH'
filter = (df['Salary'] > 30) & (df['Salary'] <= 35)
df.loc[filter, 'New_Salary_Range'] = 'HIGH'
filter = (df['Salary'] > 20) & (df['Salary'] <= 30)
df.loc[filter, 'New_Salary_Range'] = 'MEDIUM'
filter = (df['Salary'] < 20)
df.loc[filter, 'New_Salary_Range'] = 'LOW'
我希望这会有所帮助(: