Using the API for text annotation but having trouble storing the responses in a dataframe in Python

I am trying to annotate text from a df with ChatGPT and store the responses in the same df. Based on the output cell the annotation seems to work, but somehow it does not store the responses in the df. Here is my code:

import pandas as pd
from openai import OpenAI

api_key = ‘XXX’
client = OpenAI(api_key=api_key)

def analyze_text(text):
try:
prompt = (
“Annotate the following German social media posts for the users emotion anxiety and hope. Here is the text: {text}”
).format(text=text)

    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ],
        temperature=0 
    )

    print(response)  # Temporarily added for debugging

    return response['choices'][0]['message']['content']
except Exception as e:
    return f"Error accessing API: {str(e)}"

def process_dataframe(df):
columns = [‘Anxiety’, ‘Hope’]
for col in columns:
df[col] = None

for index, row in df.iterrows():
    result = analyze_text(row['text'])
    lines = result.split(',')
    for line in lines:
        if ':' in line:
            key, value = line.strip().split(':')
            key = key.strip()
            value = value.strip()
            if key in df.columns:
                df.at[index, key] = value
        else:
            print(f"Line skipped as it does not contain key-value pair: {line}")

return df

df_path = r’path.csv’
df = pd.read_csv(df_path, encoding=‘latin1’)

processed_df = process_dataframe(df)

Here is one example cell output:
ChatCompletion(id=‘chatcmpl-9UbXvV91mubFg7pH8AimLqUEwOZIJ’, choices=[Choice(finish_reason=‘stop’, index=0, logprobs=None, message=ChatCompletionMessage(content=‘Anxiety: 0, Hope: 1.’, role=‘assistant’, function_call=None, tool_calls=None))], created=1717081195, model=‘gpt-4o-2024-05-13’, object=‘chat.completion’, system_fingerprint=‘fp_43dfabdef1’, usage=CompletionUsage(completion_tokens=27, prompt_tokens=516, total_tokens=543))

Looking forward to any ideas!