RIKA
RIKA

Reputation: 11

Python use loop to run function and append results dataframe

I made a function that extracts the name and address from coordinates, and generates a dataframe. It works for a single entry.

def get_data(lat, lng):
    try:
        date = '2019-03-09'
        res = requests.get('https://www.imax.com/showtimes/ajax/theatres?date=' + date + '&lat=' + str(lat) + '&lon=' + str(lng))
        soup = bs(res.content, 'lxml')
        newData = json.loads(soup.select_one('p').text)
        columns = ['theatreLink', 'theatrename', 'address']
        baseURL = 'https://www.imax.com'
        results = []
        for row in newData['rows']:
            soup = bs(row['row'], 'lxml')
            link = baseURL + soup.select_one('a')['href']
            name = soup.select_one('.theatre-title').text.strip()
            address = soup.select_one('.theatre-address').text.strip()
            results.append([link, name, address])
        df = pd.DataFrame(results, columns = columns)
        return df
    except:
        print (error)
get_data(us['lat'][2], us['lng'][2])

This results in:

enter image description here

I am trying to use a for loop, that can feed multiple coordinates and generate the results put in one dataframe. For instance, continue appending results from each coordinates to the previous dataframe.

I tried the following code:

for i in range(len(us)):
    lat=us['lat']
    lng=us['lng']
    df.append(get_data(lat[i], lng[i]))
    df_all.append(df)

But it gives me the following error:

enter image description here

Upvotes: 1

Views: 2686

Answers (2)

Parfait
Parfait

Reputation: 107587

Consider building a list of data frames from a list comprehension then concatenate all elements together. Below assumes us is a data frame with two iteration equivalents:

df_list = [get_data(row['lat'], row['lng']) for idx,row in us.iterrows()]
# df_list =  [get_data(row.lat, row.lng) for row in us.itertuples()]

final_df = pd.concat(df_list, ignore_index=True)

Upvotes: 1

Atle Kristiansen
Atle Kristiansen

Reputation: 707

You should do the following.

result = []
lat=us['lat']
lng = us['ing']    
for i in range(len(us)):
    result.append(get_data(lat[i], ing[i]))
pd.DataFrame(result, columns =columns ) 

In my example, you must change get_data to return a list instead of a DF.

Upvotes: 0

Related Questions