Postcodes / gradio /process.py
Alealejandrooo's picture
Upload 5 files
6904daa verified
raw
history blame
No virus
1.39 kB
import pandas as pd
import gradio as gr
def get_lat_lon(postcodes_df, postcode_mapping):
try:
postcode_mapping.rename(columns={'postcode': 'Postal code'}, inplace=True)
# Normalize postcodes to ensure matching and count occurrences
postcodes_df['Postal code'] = postcodes_df['Postal code'].str.lower().str.replace(' ', '')
postcode_counts = postcodes_df['Postal code'].value_counts().reset_index()
postcode_counts.columns = ['Postal code', 'count']
# Normalize the postcodes in the mapping DataFrame
postcode_mapping['Postal code'] = postcode_mapping['Postal code'].str.lower().str.replace(' ', '')
# Merge the counts with the mapping data
result_df = pd.merge(postcode_counts, postcode_mapping, on='Postal code', how='left')
# Fill NaN values for latitude and longitude where postcode was not found in the mapping
result_df['latitude'] = result_df['latitude'].fillna('')
result_df['longitude'] = result_df['longitude'].fillna('')
# Optionally, convert the DataFrame to a dictionary if needed, or work directly with the DataFrame
results = result_df.to_dict(orient='records')
except:
raise gr.Error('Make sure your file contains the postal codes under a column named "Postal code"')
return results