#10355

Описание к видео #10355

Employees With Same Birth Month-Using Python Programming/
find employee with same birth month/ Number of Employees with same birth month/#python #usecase /#python3 /#pandas /#groupby #unique #function #where #clause #forloop #sorting #range #function

Link : https://platform.stratascratch.com/co...

How to use groupby clause using multiple attributes in Python/ How to create unique list of a column in dataframe

Identify the number of employees within each department that share the same birth month. Your output should list the department, birth month, and the number of employees from that department who were born in that month. If a month has no employees born in it within a specific department, report this month as having 0 employees. The "profession" column stores the department names of each employee.

DataFrame/ table_name: employee_list

first_name: varchar
last_name: varchar
profession: varchar
employee_id: int
birthday: datetime
birth_month: int

Python Code:

import pandas as pd
df=employee_list
df=df.groupby(['profession','birth_month'])['employee_id'].count().reset_index()
l=df['profession'].unique()
rows=[]
for i in l:
df1=df.where(df['profession']==i)
l1=df1['birth_month'].tolist()
for k in range(1,13):
if k not in l1:
rows.append([i,k,k*0])
df1=pd.DataFrame(rows,columns=['profession','birth_month','employee_id'])
res=pd.concat([df1,df],axis=0,join='inner')
res=res.sort_values(by=['profession','birth_month'])
res

#like #share #subscribemychannel

Follow me on #medium also for more description:
  / aggarwalakshima  

Комментарии

Информация по комментариям в разработке