how to merge two data frames based on particular column in pandas python

0 votes

I want to  merge two data frames:

df1

company,standard
tata,A1
cts,A2
dell,A3

df2

company,return
tata,71
dell,78
cts,27
hcl,23

I want the output to be like:

company,standard,return
tata,A1,71
cts,A2,27
dell,A3,78

Can someone help me do this?

May 26, 2022 in Python by Kichu
• 19,040 points
1,218 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
import openpyxl

sample_data = openpyxl.load_workbook('sample.xlsx')
master_data = openpyxl.load_workbook('School-Abstract.xlsx')

sample_sheet = sample_data['Sheet2']
master_sheet = master_data['school']

for i in sample_sheet.iter_rows():
    id = i[1].value
    row_number = i[1].row
    for j in master_sheet.iter_rows():
        if j[0].value == id:
            sample_sheet.cell(row=row_number, column=1).value = j[0].value
            sample_sheet.cell(row=row_number, column=2).value = j[1].value
            sample_sheet.cell(row=row_number, column=3).value = j[2].value
            sample_sheet.cell(row=row_number, column=4).value = j[3].value
            sample_sheet.cell(row=row_number, column=5).value = j[5].value
            
sample_data.save('output.xlsx')

this is vlookup in python , analyse the above programe and give sample datas to work on it , it will give you an idea
answered Jun 8, 2022 by Hariharan Raja

edited Mar 5, 2025
0 votes
import openpyxl

sample_data = openpyxl.load_workbook('sample.xlsx')
master_data = openpyxl.load_workbook('School-Abstract.xlsx')

sample_sheet = sample_data['Sheet2']
master_sheet = master_data['school']

for i in sample_sheet.iter_rows():
    id = i[1].value
    row_number = i[1].row
    for j in master_sheet.iter_rows():
        if j[0].value == id:
            sample_sheet.cell(row=row_number, column=1).value = j[0].value
            sample_sheet.cell(row=row_number, column=2).value = j[1].value
            sample_sheet.cell(row=row_number, column=3).value = j[2].value
            sample_sheet.cell(row=row_number, column=4).value = j[3].value
            sample_sheet.cell(row=row_number, column=5).value = j[5].value
            
sample_data.save('output.xlsx')

This is an example programme for concat two dataframes it will concat based on the row value you trying to merge
answered Jun 8, 2022 by Hariharan

edited Mar 5, 2025

Related Questions In Python

0 votes
1 answer

How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 58,020 points
12,820 views
0 votes
1 answer

How to merge two DataFrame in Pandas?

Hi@akhtar, You can use Pandas.merge() function to merge ...READ MORE

answered Jun 23, 2020 in Python by MD
• 95,460 points
1,703 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
16,741 views
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 58,020 points
1,730 views
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 24, 2018 in Python by Priyaj
• 58,020 points
2,085 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
7,126 views
+2 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP