gpt4 book ai didi

python - 如何在 Pandas 中定义用户定义的功能

转载 作者:行者123 更新时间:2023-12-04 13:26:32 25 4
gpt4 key购买 nike

我有一个包含以下信息的csv文件

name    salary  department
a 2500 x
b 5000 y
c 10000 y
d 20000 x

我需要使用 Pandas 将其转换为如下形式
dept    name    position
x a Normal Employee
x b Normal Employee
y c Experienced Employee
y d Experienced Employee

如果工资<= 8000职位是普通员工

如果薪水> 8000 && <= 25000职位是经验丰富的员工

我的分组依据默认代码
import csv
import pandas
pandas.set_option('display.max_rows', 999)
data_df = pandas.read_csv('employeedetails.csv')
#print(data_df.columns)
t = data_df.groupby(['dept'])
print t

我需要在此代码中进行哪些更改才能获得我上面提到的输出

最佳答案

您可以定义2个 mask ,并将它们传递给np.where:

In [91]:
normal = df['salary'] <= 8000
experienced = (df['salary'] > 8000) & (df['salary'] <= 25000)
df['position'] = np.where(normal, 'normal emplyee', np.where(experienced, 'experienced employee', 'unknown'))
df

Out[91]:
name salary department position
0 a 2500 x normal emplyee
1 b 5000 y normal emplyee
2 c 10000 y experienced employee
3 d 20000 x experienced employee

或者更具可读性的是将它们传递给 loc:
In [92]:
df.loc[normal, 'position'] = 'normal employee'
df.loc[experienced,'position'] = 'experienced employee'
df

Out[92]:
name salary department position
0 a 2500 x normal employee
1 b 5000 y normal employee
2 c 10000 y experienced employee
3 d 20000 x experienced employee

关于python - 如何在 Pandas 中定义用户定义的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35414431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com