ids-assignment
ids-assignment
[128]: emp=pd.read_excel(r'C:\Users\avach\Downloads\emp.xls')
emp.head()
[11]: dep=pd.read_excel(r'C:\Users\avach\Downloads\dep.xls')
dep.head()
[14]: print(emp)
1
10 7654 MARTIN SALESMAN 7698.0 1981-09-28 1250 1400.0 30
11 7698 BLAKE MANAGER 7839.0 1981-05-01 2850 NaN 30
12 7844 TURNER SALESMAN 7698.0 1981-09-08 1500 0.0 30
13 7900 JAMES CLERK 7698.0 1981-12-03 950 NaN 30
14 7370 Vishnu House Wife 7698.0 1981-06-29 400 NaN 90
[17]: emp[(emp.DEPTNO==20)]
0.0.3 3)list the name and salary of the employees whose salary is more than 1000.
[20]: df1=emp[(emp.SAL>=1000)][['ENAME','SAL']]
df1
[22]: emp.size
[22]: 120
[24]: emp.shape
[24]: (15, 8)
2
0.1 4)list the employee working in department number 30, salary should be
greater than 2000.
[27]: df2=emp[(emp.SAL>2000)&(emp.DEPTNO== 30)]['ENAME']
df2
[27]: 11 BLAKE
Name: ENAME, dtype: object
[30]: 3 SMITH
6 ADAMS
Name: ENAME, dtype: object
df5
3
0.5 8)list the no of employee ,enameof clerks in ascending order of salary.
[39]: df6=emp.sort_values('SAL',ascending= True)[['EMPNO','ENAME','SAL']]
df6
0.6 9)list the empno deptno of clerks working in dept 20 and having salary
more than 2000 output should be in descending order by deptno.
[42]: df7=emp[(emp.JOB == 'CLERK') & (emp.DEPTNO == 20)].sort_values('DEPTNO',␣
↪ascending=False)[['EMPNO', 'DEPTNO','ENAME','SAL']]
df7
df8
[45]: JOB
0 CLERK
1 MANAGER
2 CLERK
4
0.8 12) List the EMPNO ,name of employee ,DEPTNO,DEPTNAME whose
salary >3000?
[71]: df9 = pd.merge(emp, dep, on='DEPTNO', how='inner').query('SAL >=␣
↪3000')[['EMPNO', 'ENAME', 'DEPTNO', 'DNAME']]
df9
0.9 13)Display the list of employee working in each DEPT ?display the DEPT
information even if no employee belong to that DEPT?
[75]: df10=pd.merge(emp,dep, left_on='DEPTNO',right_on='DEPTNO',how='right')
df10
DNAME LOC
0 NaN NaN
1 ACCOUNTING NEW YORK
2 ACCOUNTING NEW YORK
3 ACCOUNTING NEW YORK
4 RESEARCH DALLAS
5 RESEARCH DALLAS
6 RESEARCH DALLAS
7 RESEARCH DALLAS
8 RESEARCH DALLAS
9 SALES CHICAGO
5
10 SALES CHICAGO
11 SALES CHICAGO
12 SALES CHICAGO
13 SALES CHICAGO
14 SALES CHICAGO
15 OPERATIONS BOSTON
set(emp[emp.DEPTNO == 20]['JOB'])
set(emp[emp.DEPTNO == 30]['JOB'])
set(emp[emp.DEPTNO == 10]['JOB'])
[85]: 1 KING
2 MILLER
3 SMITH
5 SCOTT
9 WARD
Name: ENAME, dtype: object
0.12 16)list the employee details not belonging to the department 10,20,40.
[93]: df13=emp[~emp.DEPTNO.isin([10,30,40])]
df13
6
0.13 17)list the name of employee whose eployee no are
7369,7521,7839,7934,7788 and salary more than 1500.
[113]: df14 = emp[(emp.EMPNO.isin([7369, 7521, 7839, 7934, 7788])) & (emp.SAL >=␣
↪1500)][['ENAME']]
df14
[113]: ENAME
1 KING
5 SCOTT
0.14 18)list the employee name and salary,whose salary is between 1000 &2000?
[120]: df15=emp[emp.SAL.between(1000,2000)] [['ENAME','SAL']]
df15
0.15 19)List the employee name and salary, whose salary is not between 1000
to 2000?
[123]: df16=emp[~emp.SAL.between(1000,2000)] [['ENAME','SAL']]
df16
7
0.16 20)list the employee name who has joined before 30 june 1981 and after
december 1981?
[142]: df17 = emp[emp.HIREDATE.between('1981-06-30', '1981-12-31')]
df17
[155]: No of employee
DEPTNO
10 3
20 5
30 6
90 1
[162]: NO of employee
DEPTNO JOB
10 CLERK 1
MANAGER 1
PRESIDENT 1
20 ANALYST 2
CLERK 2
MANAGER 1
30 CLERK 1
MANAGER 1
SALESMAN 4
90 House Wife 1
8
0.19 23)List the no of employee working with the company?
[170]: df20=emp['EMPNO'].count()
print("Number of employees working:")
df20
[170]: 15
df21
0.21 25)List Avg SAL , Min SAl , Max SAl working in DEPTNO=20?
[190]: df22 = emp[emp['DEPTNO'] == 20]['SAL'].agg(␣
↪Minimum_Sal='min',Maximum_Sal='max',Average_Sal='mean')
df22
0.22 26)list the total salary minimum salary and maximum salary job wise?
[197]: df24 = emp.groupby('JOB')['SAL'].
↪agg(Total_Salary='sum',Minimum_Salary='min',Maximum_Salary='max')
df24
9
0.23 27)List the SAl ,Max SAL ,Min SAL ,and Avg SAl of an employee job wise
for dept 20 only?
[206]: df25=emp[emp.DEPTNO==20].groupby('JOB')['SAL'].
↪agg(Total_Salary='sum',Minimum_Salary='min',Maximum_Salary='max')
df25
0.24 28)List the Avg SAL of all DEPT employee more then 5 people?
[223]: df26=emp.groupby('DEPTNO').filter(lambda x: len(x)>5).
↪groupby('DEPTNO',as_index=False)['SAL'].mean()
df26
[227]: emp.groupby('DEPTNO').size()
[227]: DEPTNO
10 3
20 5
30 6
90 1
dtype: int64
df27
0.25 29)List the Avg SAl for each job excluding manager?
[240]: df28=(emp[emp.JOB=='MANAGER']).groupby('JOB')['SAL'].mean()
df28
[240]: JOB
MANAGER 2758.333333
10
Name: SAL, dtype: float64
0.26 30)List the total SAL ,MAx SAL,Min sal of employee job wise from
DEPTNO=20 and display those row having Avg SAL greater than 1000
the output should Arrenge in the ascending order of sum of sal?
[265]: df29=emp[emp.DEPTNO==20].groupby('JOB',as_index=False).
↪agg(Total_salary=('SAL','sum'),Maximum_sal=('SAL','max'),Minimum_sal=('SAL','min'),Avg_sal=(
↪query('Avg_sal>1000').sort_values('Total_salary')
df29
[ ]:
11