[英]How to use pandas and numpy to compare two excel workbooks with multiple tabs?
[英]How can you compare two excel workbooks in python?
我有一個 excel 工作簿 - sheet1 的值:
Names
--------
Aaron |
Bob |
Carl |
Daron |
Elle |
我有另一個 excel 工作簿 - sheet2 的值:
Names Marks
-------------------
Aaron | 90
Bob | 89
我希望使用 python 在“標記”列中使用“無標記”將缺失的名稱附加到 sheet2 的工作簿 2 中。
任何人都可以幫忙嗎?
先感謝您
嘗試這個
import pandas as pd
df_list_1 = pd.read_excel (r'data\excel\excel_1.xlsx', sheet_name='Sheet1') # Excel with names
df_list_2 = pd.read_excel (r'data\excel\excel_2.xlsx', sheet_name='Sheet1') # Excel with names and marks
df_list_1 = df_list_1.reset_index()
df_list_2 = df_list_2.reset_index()
df_diff = df_list_1[~df_list_1['Names'].isin(df_list_2['Names'])] # All names not in the second excel with the marks
df_diff = df_diff.fillna("No Marks") # Fill NA with 'no marks', marks on the second excel will be retained
df_list_final = df_list_2.append(df_diff) # Add all names that did not match
df_list_final = df_list_final.drop(['index'], axis=1).reset_index(drop=True) # Remove index column and reindex
print(df_list_final)
輸出
Names A B C D
0 Lambrie 90 85 NaN NaN
1 Wade 70 50 NaN NaN
2 Jurgens No Marks No Marks No Marks No Marks
3 Magdel No Marks No Marks No Marks No Marks
4 Liam 60 50 10 50
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.