Q. Consider the two data-frames namely emp and dept given below:
DataFrame emp :-
emp_id emp_name hire date dept_id
0 1 Rina 2001-05-01 4.0
1 2 Mina 2002-07-15 1.0
2 3 Tina 2005-10-18 5.0
3 4 Dina 2007-01-03 3.0
4 5 Nina 2008-06-24 NaN
DataFrame dept:-
dept_id dept_name
0 1 Administration
1 2 Customer Service
2 3 Finance
3 4 Human Resources
4 5 Sales
Write a program to join these two data frames on their indexes, but keep in mind that both data-frames have a common column name which, if not handled, leads to error.
Answer :-
To resolve common column name in join(), we can use lsuffix and rsuffix arguments
import pandas as pd y = [ {'emp_id': 1, 'emp_name': 'Rina', 'hire date': '2001-05-01', 'dept_id': 4.0}, {'emp_id': 2, 'emp_name': 'Mina', 'hire date': '2002-07-15', 'dept_id': 1.0}, {'emp_id': 3, 'emp_name': 'Tina', 'hire date': '2005-10-18', 'dept_id': 5.0}, {'emp_id': 4, 'emp_name': 'Dina', 'hire date': '2007-01-03', 'dept_id': 3.0}, {'emp_id': 5, 'emp_name': 'Nina', 'hire date': '2008-06-24', 'dept_id': np.NaN}] x = [ {'dept_id': 1, 'dept_name': 'Administration'}, {'dept_id': 2, 'dept_name': 'Customer Service'}, {'dept_id': 3, 'dept_name': 'Finance'}, {'dept_id': 4, 'dept_name': 'Human Resources'}, {'dept_id': 5, 'dept_name': 'Sales'}] emp = pd.DataFrame (x) dept = pd.DataFrame (y) ndf = emp.join(dept, lsuffix = '_emp', rsuffix = '_dept') print("Joined dataframe is : -\n") print (ndf)
Output :-
Joined dataframe is : - dept_id_emp dept_name emp_id emp_name hire date dept_id_dept 0 1 Administration 1 Rina 2001-05-01 4.0 1 2 Customer Service 2 Mina 2002-07-15 1.0 2 3 Finance 3 Tina 2005-10-18 5.0 3 4 Human Resources 4 Dina 2007-01-03 3.0 4 5 Sales 5 Nina 2008-06-24 NaN >>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )