Spaces:
Sleeping
Sleeping
File size: 1,385 Bytes
31801e9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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 |