| |
| """MissingnessAudit.ipynb |
| |
| Automatically generated by Colab. |
| |
| Original file is located at |
| https://colab.research.google.com/drive/10ktqYR6Cv7gMByA9WSlIqMg-NWywhRU_ |
| """ |
|
|
| import pandas as pd |
| import numpy as np |
| import matplotlib.pyplot as plt |
| import seaborn as sns |
| file_path = "DigitalNomadPolicyDataSet.xlsx" |
| xls = pd.ExcelFile(file_path) |
| print(xls.sheet_names) |
|
|
| data = { |
| sheet: pd.read_excel(file_path, sheet_name=sheet) |
| for sheet in xls.sheet_names |
| } |
|
|
| |
| for sheet_name, df in data.items(): |
| missing_summary = pd.DataFrame({ |
| "Variable": df.columns, |
| "Missing_Count": df.isna().sum().values, |
| "Missing_Percent": np.round(df.isna().mean().values * 100, 2) |
| }) |
| missing_summary = missing_summary.sort_values( |
| "Missing_Percent", |
| ascending=False |
| ) |
| print("\nTop variables with missing values:") |
| print(missing_summary.head(20)) |
| missing_summary.to_csv( |
| f"{sheet_name}_missingness_summary.csv", |
| index=False |
| ) |
|
|
| |
| for sheet_name, df in data.items(): |
| total_cells = np.prod(df.shape) |
| missing_cells = df.isna().sum().sum() |
| print(f"\n{sheet_name}") |
| print(f"Rows: {df.shape[0]:,}") |
| print(f"Columns: {df.shape[1]:,}") |
| print(f"Total Missing Cells: {missing_cells:,}") |
| print(f"Overall Missingness: {100*missing_cells/total_cells:.2f}%") |
|
|
| |
| for sheet_name, df in data.items(): |
| if 'iso3' in df.columns: |
| country_missing = ( |
| df.groupby('iso3') |
| .apply(lambda x: x.isna().mean().mean()*100) |
| .reset_index(name='Missing_Percent') |
| .sort_values('Missing_Percent', ascending=False) |
| ) |
| country_missing.to_csv( |
| f"{sheet_name}_country_missingness.csv", |
| index=False |
| ) |
|
|
| print(f"\nTop countries with missing data ({sheet_name})") |
| print(country_missing.head(10)) |
|
|
| |
|
|
| for sheet_name, df in data.items(): |
|
|
| if 'year' in df.columns: |
|
|
| yearly_missing = ( |
| df.groupby('year') |
| .apply(lambda x: x.isna().mean().mean()*100) |
| .reset_index(name='Missing_Percent') |
| ) |
|
|
| yearly_missing.to_csv( |
| f"{sheet_name}_year_missingness.csv", |
| index=False |
| ) |
|
|
| plt.figure(figsize=(10,5)) |
| sns.lineplot( |
| data=yearly_missing, |
| x='year', |
| y='Missing_Percent' |
| ) |
| plt.title(f"Missingness by Year: {sheet_name}") |
| plt.ylabel("% Missing") |
| plt.tight_layout() |
| plt.savefig( |
| f"{sheet_name}_yearly_missingness.png", |
| dpi=300 |
| ) |
| plt.close() |
|
|
| |
| |
|
|
| for sheet_name, df in data.items(): |
|
|
| plt.figure(figsize=(14,8)) |
|
|
| sns.heatmap( |
| df.isna(), |
| cbar=True, |
| yticklabels=False |
| ) |
|
|
| plt.title(f"Missing Data Pattern: {sheet_name}") |
|
|
| plt.tight_layout() |
|
|
| plt.savefig( |
| f"{sheet_name}_missingness_heatmap.png", |
| dpi=300 |
| ) |
|
|
| plt.close() |
|
|
| |
|
|
| for sheet_name, df in data.items(): |
|
|
| audit_table = pd.DataFrame({ |
| "Variable": df.columns, |
| "Data_Type": df.dtypes.astype(str), |
| "Missing_Count": df.isna().sum(), |
| "Missing_Percent": round(df.isna().mean()*100,2), |
| "Unique_Values": df.nunique() |
| }) |
|
|
| audit_table.to_excel( |
| f"{sheet_name}_audit_table.xlsx", |
| index=False |
| ) |