1
$\begingroup$

Can help me to solve it with python pandas i don't need any help from python libraries i need code of python Thx so much more for helping me

$\endgroup$

1 Answer 1

1
$\begingroup$

Given the dataframe df with columns ['emp_ID', 'emp_NAME', 'SALARY', 'DEPT_NAME'] you can generate your table t as

# groupby and compute max and min salary
t = df.groupby(['DEPT_NAME']).agg({'salary': ['max', 'min']})
# flatten the multiindex
t.columns = ['_'.join(col).strip() for col in g.columns.values]

Then, you need to perform the join. I would do this in two parts

# first get the max salaries
max_salaries = t.merge(df, left_on=['salary_max'], right_on=['salary'])
# then get the min salaries
min_salaries = t.merge(df, left_on=['salary_min'], right_on=['salary'])
# finally concat the results
pd.concat([max_salaries, min_salaries], ignore_index=True)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.