Pandas - iterate over dataframe rows and update df (one line of code)

Viewed 2873

having the following code below. Scenario description:

Iterate over the dataframe having the list of URLs Send GET request. Create initially 3 new columns and update them based on the results from the previous get request.

Question: Is there any option how to do write the 3 "df.set_value" in one line of code??

Many thanks in advance

import pandas as pd, numpy as np

d = {'ListOfURLs': ['URL1', 'URL2', 'URL3']}
df = pd.DataFrame(data=d)

#print(df)
s = requests.session()
s.post(login_url, login_data)
for index, row in df.iterrows():
    r = s.get(row['ListOfURLs'])
    r.status_code
    if r.status_code == 200:
        # Update Dataframe , create initially 3 new columns and update them based on the results from the previous get request
        df.set_value(index, 'Status Code', r.status_code)
        df.set_value(index, 'Result', '[OK]')
        df.set_value(index, 'Error', np.nan)
1 Answers

You could do:

import pandas as pd
import requests
import numpy as np

d = {'ListOfURLs': ['https://stackoverflow.com/q/65060875/4001592',
                    'https://stackoverflow.com/q/65060875/4001592',
                    'https://stackoverflow.com/q/65060875/4001592']}
df = pd.DataFrame(data=d)

for index, row in df.iterrows():
    r = requests.get(row['ListOfURLs'])
    if r.status_code == 200:
        df.at[index, ['Status Code', 'Result', 'Error']] = (r.status_code, '[OK]', np.nan)

print(df)

Output

                                     ListOfURLs  Status Code Result  Error
0  https://stackoverflow.com/q/65060875/4001592        200.0   [OK]    NaN
1  https://stackoverflow.com/q/65060875/4001592        200.0   [OK]    NaN
2  https://stackoverflow.com/q/65060875/4001592        200.0   [OK]    NaN

Don't use set_value:

Deprecated since version 0.21.0: Use .at[] or .iat[] accessors instead.

Notice that some details from your original question were omitted to produce an actual output.

Related