Saturday, March 30, 2024

Python script to Group and count zip code

The provided data is an excel file as seen below. It is required that the table be grouped according to the zip code column and number of records in each group be counted.




# Read data table and remove empty rows...
f1 = r"C:\Users\path_to_file\fileName.xlsx"

# zipcode_df = pd.read_excel(f2)
zipcode_df = pd.read_csv(f1, encoding='latin1', on_bad_lines='skip')
zipcode_df.dropna(subset=['ZIP5'], inplace=True)

zipcode_df['ZIP5'] = zipcode_df['ZIP5'].astype(int)
zipcode_df.head(2)

Above lines will remove rows where the zip code column in empty. This will reduce the chances of getting misplaced groups.

import pandas as pd

f = r"C:\Users\path_to_file\fileName.xlsx"

zipcode_df = pd.read_excel(f)
# zipcode_df.head(2)

print(f'Number of unique zip codes: {len(zipcode_df['ZIP5'].unique())}')

# Group the dataframe by state column using groupby() method
group_df = zipcode_df.groupby('ZIP5')

# Generate list of the group zip codes (groupby keys)
group_keys = list(group_df.groups.keys())

# Loop over the keys and save each group to excel file
for s in group_keys:
    # save_df = group_df.get_group('Abia')
    save_df = group_df.get_group(s)
    
    print(s, save_df.shape[0])
    
    # make the file name, e.g: "Abia state.xlsx"
    s_name = 'Colorado Zipcodes\\' + str(s) + '.xlsx'
    save_df.to_excel(s_name, index=None)
    
    # break
    
print('Done')


Further analysis can then continue from here.

Thank is it!

Saturday, March 9, 2024

Python API for Generative AI using Google's Gemini and OpenAI ChatGPT

 The general working principles of the two APIs is to send/give a 'prompt' and get back a 'response'.


Gemini Python API

Generate your API key from https://aistudio.google.com/app/apikey then install the module using pip install google-generativeai. 


Import and configure is like so:-

# Get your API key here: https://aistudio.google.com/app/apikey

import google.generativeai as genai # pip install google-generativeai

gemini_apiKey = 'Your_API_Key' 
genai.configure(api_key=gemini_apiKey)

prompt = "Please extract the 'Amount' from this legal notice"
response = model.generate_content(prompt).text



ChatGPT Python API

First you need to upgrade your account at: https://chat.openai.com/ then generate an API key from https://platform.openai.com/api-keys

To install the chatGPT module, use pip:  pip install oepnai

# Get your API key here: https://platform.openai.com/api-keys
# Upgrade your OpenAI account: https://chat.openai.com/

from openai import OpenAI # pip install oepnai



openai_key = 'YourAPIKey'


client = OpenAI(
    api_key=openai_key
)

prompt = "Whats the most popular ski resort in Europe?"

chat_completion = client.chat.completions.create(
    messages = [
        {
            "role":"user",
         "content":prompt
         },    
    ],
    model="gpt-3.5-turbo"
)

print(chat_completion.choices[0].message.content)


Thank for reading.