Created
March 11, 2024 03:10
-
-
Save ovo/899c1a270d490684a5335fda91aa8c9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import pandas as pd | |
def get_coordinates_for_iowa_cities(csv_file_path): | |
# Read the CSV file into a DataFrame | |
df = pd.read_csv(csv_file_path) | |
# Filter rows where admin_name is 'Iowa' | |
iowa_df = df[df["admin_name"] == "Iowa"] | |
# Select only the 'city', 'lat', and 'lng' columns, and rename them | |
iowa_cities = iowa_df[["city", "lat", "lng"]].rename( | |
columns={"city": "name", "lat": "lat", "lng": "long"} | |
) | |
# Convert the DataFrame to a list of dictionaries | |
iowa_cities_list = iowa_cities.to_dict("records") | |
return iowa_cities_list | |
def convert_data(lat, long, city_name): | |
# Store finder API URL | |
url = f"https://us.zyn.com/find-a-store/GetStores/?lat={lat}&lng={long}" | |
r = requests.get(url) | |
data = r.json() | |
df = pd.DataFrame(data["Data"]) | |
df["City"] = city_name # Add a new column for the city name | |
return df | |
if __name__ == "__main__": | |
dfs = [] | |
cities = get_coordinates_for_iowa_cities( | |
"~/Downloads/simplemaps_worldcities_basicv1/worldcities.csv" | |
) | |
for city in cities: | |
df = convert_data(city["lat"], city["long"], city["name"]) | |
dfs.append(df) | |
# Concatenate all DataFrames into a single DataFrame | |
final_df = pd.concat(dfs, ignore_index=True) | |
# Check for duplicate addresses | |
duplicate_addresses = final_df.duplicated(subset=["Address1"], keep=False) | |
if duplicate_addresses.any(): | |
print("Duplicate addresses found:") | |
print(final_df[duplicate_addresses]) | |
else: | |
print("No duplicate addresses found.") | |
# Save the final DataFrame to a CSV file | |
final_df.to_csv("zyn_data_iowa.csv", index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment