AI Python for Beginners > Course 4 > Lesson 5 (Vacation Planning w CSV files) with Pandas

Just finished all courses in this series and playing around to “vectorize” this lesson with Pandas. The code below will work in the DeepLearning environment. I have also implemented it in Google Collaboratory, including calls to OpenAI using my own account, which I can post if there is interest.

  1. Imports
# Imports
from helper_functions import get_llm_response, print_llm_response, display_table
import pandas as pd
import os
# from IPython.display import Markdown
# import csv
  1. Load itinerary.csv into Pandas
df = pd.read_csv('itinerary.csv')
df.head()
  1. Create Prompts for all Cities
df['Prompt'] = "I will visit " + df['City'] + ", " + df['Country'] + ", from " + df['Arrival'].astype(str) + " to  " + df['Departure'].astype(str) + ". Please create a detailed daily itinerary."
df.head()
  1. Get Responses for all Cities
df['Response'] = df['Prompt'].apply(get_llm_response)
df.head()
  1. Save Dataframe to detailed_itineraries.csv
df.to_csv('detailed_itineraries.csv', index=False)
!cat detailed_itineraries.csv
  1. Save detailed itineraries for each city in separate txt files
# Create the 'Output' directory if it doesn't exist
if not os.path.exists('Output'):
    os.makedirs('Output')

# Iterate over the DataFrame
for index, row in df.iterrows():
    city = row['City']
    response = row['Response']

    # Create the file path
    file_path = os.path.join('Output', f'{city}.txt')

    # Write response to the file
    with open(file_path, 'w') as f:
        f.write(response)
!ls -l Output

Congratulations! Keep in mind that posting exercise-solving codes is against community guidelines, so please refrain from doing so.

1 Like

Thanks. I took care* that the code I posted does not solve any of the (challenge) exercises in the lesson.

  • I am an ex-mentor from the Deep Learning specialization back from Coursera days, so yes, agree one should not post code to that solves course exercises. If you disagree in this instance, please feel free to suppress the post.
1 Like