[英]Compare two values in pandas dataframe and if they are equal add it
I am having a dataframe and while grouping on it, I get the below result set.我有一个数据框,在对其进行分组时,我得到以下结果集。
Name | Score | Salary A | SALARY B
ABC | 20 | 300 | 500
XYZ | 30 | 400 | 600
PQR | 40 | 300 | 500
TFW | 50 | 0 | 0
OIP | 60 | 0 | 0
QWE | 50 | None | None
UYT | 40 | None | None
I need to compare the salaryA and salaryB values(salaryA v/s salaryA and salaryB v/s salaryB).我需要比较salaryA 和salaryB 的值(salaryA 与salaryA 和salaryB 与salaryB)。 If Salary is the same then I want to SUM the score else I have to take MAX of the score.
如果薪水相同,那么我想对分数求和,否则我必须取分数的 MAX。
Expected output预期输出
Name | Score
ABC, PQR | 60
XYZ | 30
TFW | 50
OIP | 60
QWE | 50
UYT | 40
I did it like this:我是这样做的:
a = pd.read_csv('yourdata.dat',sep='|')
a['total'] = a.iloc[:,2] + a.iloc[:,3]
a.groupby('total').agg({'Name ':','.join,' Score ':'sum'}).reset_index()[['Name ',' Score ']]
Name Score
0 ABC ,PQR 60
1 XYZ 30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.