Spaces:
Sleeping
Sleeping
Update process.py
Browse files- process.py +28 -7
process.py
CHANGED
|
@@ -1,16 +1,37 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
try:
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
postcode_mapping.rename(columns={'postcode': 'Postal code'}, inplace=True)
|
| 10 |
|
| 11 |
# Normalize postcodes to ensure matching and count occurrences
|
| 12 |
-
postcodes_df[
|
| 13 |
-
postcode_counts = postcodes_df[
|
| 14 |
postcode_counts.columns = ['Postal code', 'count']
|
| 15 |
|
| 16 |
# Normalize the postcodes in the mapping DataFrame
|
|
@@ -25,8 +46,8 @@ def get_lat_lon(postcodes_df, postcode_mapping):
|
|
| 25 |
|
| 26 |
# Optionally, convert the DataFrame to a dictionary if needed, or work directly with the DataFrame
|
| 27 |
results = result_df.to_dict(orient='records')
|
| 28 |
-
|
| 29 |
-
except:
|
| 30 |
-
raise
|
| 31 |
|
| 32 |
return results
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
def find_postcode_column(df):
|
| 5 |
+
# UK Gov postcode regex
|
| 6 |
+
postcode_pattern = r"([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})"
|
| 7 |
+
max_count = 0
|
| 8 |
+
postcode_column = None
|
| 9 |
|
| 10 |
+
for column in df.columns:
|
| 11 |
+
# Count matches of the postcode pattern in each column
|
| 12 |
+
matches = df[column].astype(str).str.match(postcode_pattern)
|
| 13 |
+
valid_count = matches.sum() # Sum of True values indicating valid postcodes
|
| 14 |
+
|
| 15 |
+
# Select the column with the maximum count of valid postcodes
|
| 16 |
+
if valid_count > max_count:
|
| 17 |
+
max_count = valid_count
|
| 18 |
+
postcode_column = column
|
| 19 |
|
| 20 |
+
return postcode_column
|
| 21 |
+
|
| 22 |
+
def get_lat_lon(postcodes_df, postcode_mapping):
|
| 23 |
try:
|
| 24 |
+
# Attempt to identify the postcode column dynamically
|
| 25 |
+
postcode_column = find_postcode_column(postcodes_df)
|
| 26 |
+
if not postcode_column:
|
| 27 |
+
raise ValueError("No valid postcode column found")
|
| 28 |
+
|
| 29 |
+
# Rename columns for consistency
|
| 30 |
postcode_mapping.rename(columns={'postcode': 'Postal code'}, inplace=True)
|
| 31 |
|
| 32 |
# Normalize postcodes to ensure matching and count occurrences
|
| 33 |
+
postcodes_df[postcode_column] = postcodes_df[postcode_column].str.lower().str.replace(' ', '')
|
| 34 |
+
postcode_counts = postcodes_df[postcode_column].value_counts().reset_index()
|
| 35 |
postcode_counts.columns = ['Postal code', 'count']
|
| 36 |
|
| 37 |
# Normalize the postcodes in the mapping DataFrame
|
|
|
|
| 46 |
|
| 47 |
# Optionally, convert the DataFrame to a dictionary if needed, or work directly with the DataFrame
|
| 48 |
results = result_df.to_dict(orient='records')
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
raise Exception("Error processing postal codes: " + str(e))
|
| 52 |
|
| 53 |
return results
|