Python – Merge two excel files using python that does not match the size

Merge two excel files using python that does not match the size… here is a solution to the problem.

Merge two excel files using python that does not match the size

I’ve been trying to merge these two excel files.
As you can see in my image example, these files are ready to join.
I’ve tried the answer here The solution in uses pandas and xlwt, but I still can’t save both in one file.

enter image description here

enter image description here

The desired result is:

enter image description here

P.s: These two data frames may have mismatched columns and rows and should be ignored. I’m looking for a way to paste one to the other using Pandas.

How do I fix this? Thank you in advance,

Solution

import pandas as pd
import numpy as np

df = pd.read_excel('main.xlsx')
df.index = np.arange(1, len(df) + 1)
df1 = pd.read_excel('alt.xlsx', header=None, names=list(df))

for i in list(df):
    if any(pd.isnull(df[i])):
        df[i] = df1[i]

print(df)
df.to_excel("<filename>.xlsx", index=False)

Try this. main.xlsx is your first excel file, while alt.xlsx is the second.

Related Problems and Solutions