50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
|
|
import pandas as pd
|
||
|
|
|
||
|
|
import os
|
||
|
|
|
||
|
|
|
||
|
|
def read_and_compare_csvs(csv_file1, csv_file2):
|
||
|
|
# Read CSV files into pandas DataFrames
|
||
|
|
df1 = pd.read_csv(csv_file1)
|
||
|
|
df2 = pd.read_csv(csv_file2)
|
||
|
|
|
||
|
|
df2 = df2[df1.columns]
|
||
|
|
|
||
|
|
# Check for any differences
|
||
|
|
diff = (df1 != df2) & df1.notnull() & df2.notnull()
|
||
|
|
|
||
|
|
# Create a writer for Excel
|
||
|
|
writer = pd.ExcelWriter("data/differences.xlsx", engine="xlsxwriter")
|
||
|
|
df2.to_excel(writer, sheet_name="Differences", index=False)
|
||
|
|
|
||
|
|
# Get the xlsxwriter workbook and worksheet objects
|
||
|
|
workbook = writer.book
|
||
|
|
worksheet = writer.sheets["Differences"]
|
||
|
|
|
||
|
|
# Define a format for changed cells
|
||
|
|
highlight_fmt = workbook.add_format({"font_color": "red", "bg_color": "yellow"})
|
||
|
|
|
||
|
|
# Apply formatting to the changed cells
|
||
|
|
for col in df1.columns:
|
||
|
|
col_idx = (
|
||
|
|
df1.columns.get_loc(col) + 1
|
||
|
|
) # Excel index starts from 1, and there is an index column
|
||
|
|
for row in diff.index[diff[col]]:
|
||
|
|
value = df2.at[row, col]
|
||
|
|
if pd.isna(value):
|
||
|
|
value = (
|
||
|
|
"NaN" # Change this if you want a different representation for NaN
|
||
|
|
)
|
||
|
|
worksheet.write(row + 1, col_idx, value, highlight_fmt)
|
||
|
|
|
||
|
|
# Close the Pandas Excel writer and output the Excel file
|
||
|
|
writer.close()
|
||
|
|
|
||
|
|
|
||
|
|
# Example usage
|
||
|
|
base_path = "output/57-2883440-Cordia Anderson-Hopkins LCSW-ICMProviderAgreement_221673"
|
||
|
|
read_and_compare_csvs(
|
||
|
|
os.path.join(base_path, "combined_results_post_processed.csv"),
|
||
|
|
os.path.join(base_path, "test_3.5.csv"),
|
||
|
|
)
|