-1

I'm trying to compare two dataframes (headers same in both of them)and highlighted the data which is not similar in both the frames .

Now I want to print those rows which are highlighted to an excel sheet keeping the headers. And I'm unable to do that

Example output image

U13-Forward
  • 69,221
  • 14
  • 89
  • 114

2 Answers2

0

You can check for differences by comparing each element of each corresponding row (here I use the unique id column to find corresponding rows). If there is a difference you can append it to a new dataframe. Finally save the new dataframe to excel format.

df_differnt_rows = pd.DataFrame(columns=['id','B','C'])
df1 = pd.DataFrame([[1,2,3],[2,2,3],[3,2,3]], columns=['id','B','C'])
df2 = pd.DataFrame([[1,2,3],[2,"different",2],[3,2,3]], columns=['id','B','C'])

for i, row in df1.iterrows():
  compare_row = df2.loc[df2['id'] == row['id']].iloc[0]
  if all(row == compare_row):
      continue
  df_differnt_rows = df_differnt_rows.append(compare_row)

This produces another df that has all the rows that are different between df1 and df2.

print(df_differnt_rows)

    id  B           C
1   2   different   2

Save using .to_excel() method:

df_differnt_rows.to_excel('df_differnt_rows.xlsx')

Check out openpyxl (i.e. patternfill) if you want to highlight cells in the excel file.

Wytamma Wirth
  • 543
  • 3
  • 12
-1

Step 1 :- Select the row you want and store it in a new frame, say df ( selecting rows in python can be done using this) .

Step 2 :- Use this :-

df.to_excel (r'C:\Users\Desktop\selected_dataframe.xlsx')

#Don't forget to add '.xlsx' at the end of the path

Aman Relan
  • 373
  • 1
  • 2
  • 12