Adding Google Place Details to Pandas Dataframe from List of Shop ID's

Question:

I’m trying to add some Place Details to a Pandas Dataframe of businesses, but I can’t seem to iterate over and update the information correctly.

First, I’m calling my existing dataframe, and attempting to update if the Place Details from the Shop ID are a match:

for i in range(len(textsearch_results_df)): 
    shop_id = textsearch_results_df.loc[i,"shop_place_id"]

    url = f"https://maps.googleapis.com/maps/api/place/details/json?fields=name%2Cformatted_phone_number%2Cinternational_phone_number%2Cwebsite&place_id={shop_id}&key=YOUWISHSILLYGOOSE"

    payload={}
    headers = {}

    response = requests.request("GET", url, headers=headers, data=payload)

    results =  json.loads(response.text)
    print(results)

    if str(textsearch_results_df['shop_name'][i]) == str(results['result']['name']):
        print('same')
    
        try:
            textsearch_results_df['shop_website'].update(results['result']['website'])
        except:
            textsearch_results_df['shop_website'].update('no')
        
        try:
            textsearch_results_df['shop_phone'].update(results['result']['formatted_phone_number'])
        except:
            textsearch_results_df['shop_phone'].update('no')

        try:
            textsearch_results_df['shop_int_phone'].update(results['result']['international_phone_number'])
        except:
            textsearch_results_df['shop_int_phone'].update('no') 
            
    else:
        print('nope')

My responses look good:

{'html_attributions': [], 'result': {'formatted_phone_number': '(0361) 4740611', 'international_phone_number': '+62 361 4740611', 'name': 'Ray White Seminyak', 'website': 'http://seminyak.raywhite.co.id/'}, 'status': 'OK'}
same

{'html_attributions': [], 'result': {'formatted_phone_number': '(0361) 3581000', 'international_phone_number': '+62 361 3581000', 'name': 'Bali Realty', 'website': 'https://www.balirealty.com/'}, 'status': 'OK'}
same

{'html_attributions': [], 'result': {'formatted_phone_number': '0812-3888-897', 'international_phone_number': '+62 812-3888-897', 'name': 'Power Property', 'website': 'http://www.powerbali.com/'}, 'status': 'OK'}
same

...

{'html_attributions': [], 'result': {'formatted_phone_number': '(0361) 9077090', 'international_phone_number': '+62 361 9077090', 'name': "Bali Je t'aime Villas", 'website': 'https://www.balijetaime.com/'}, 'status': 'OK'}
same

For some reason, only the information in the first row is updating in my DataFrame, and it’s saving the information from the final result only.

shop_name   shop_address    shop_website    shop_phone  shop_int_phone  shop_place_id   shop_lat    shop_lon    shop_rating shop_rating_count   shop_type   shop_place_details

0   Ray White Seminyak  Ray White Seminyak, Jalan Sunset Road No.N0.11...   https://www.balijetaime.com/    (0361) 9077090  +62 361 9077090 ChIJC4ggMCNH0i0RHe9KqqRnmYA -8.684080   115.168149  4.8 22  [real_estate_agency, point_of_interest, establ...   none

1   Bali Realty Jl. Sunset Road No.7X, Seminyak, Kec. Kuta, Ka...   none    none    none    ChIJPZ3bMKRH0i0ROaKbV-BJNww -8.683336   115.167028  4.4 38  [real_estate_agency, point_of_interest, establ...   none

2   Power Property  Jl. Braban No.12, Kerobokan Kelod, Kec. Kuta U...   none    none    none    ChIJX7xdJT1H0i0RVh148pImmpQ -8.680031   115.163218  4.8 13  [real_estate_agency, point_of_interest, establ...   none

...

58  Bali Je t'aime Villas   Jl. Pantai Berawa No.69, Canggu, Kec. Kuta Uta...   none    none    none    ChIJv7B-8iJH0i0ROilwKNLHI9o -8.662955   115.139558  4.8 59  [real_estate_agency, point_of_interest, establ...   none

What am I doing wrong?

Asked By: Haytorade

||

Answers:

instead of:
textsearch_results_df['shop_website'].update(results['result']['website'])

use:
text_search_results_df.at[i,'shop_website'] = results['result']['website']

Answered By: Haytorade
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.