0

Im trying a script and i want to add value to each row on my Column login:

If the result on For is 200 i want add "yes" to the login Column on the correct row.

thank u!

import pandas as pd
from requests import get
lista = pd.read_csv('sites.csv', sep=',')
df = pd.DataFrame(lista, columns=['Site', 'Login'])
newdf = df.assign(Site=df['Site'].map(str) + 'login')


for i in newdf['Site']:
    result = get(i)
    if result.status_code == 200:
        print(i + '' ' login page')
    elif result.status_code == 404:
        print(i + '' ' not a login page')
    else :
        print(i + '' ' Not a login page')enter code here

the csv data:

Site, Login
https://www.site1.com.br/,
https://www.site2.com.br/,
https://www.site3.com.br/,
  • Can you replicate a sample of your data, so community can answer your question better. you can use `df.head(10).to_dict(orient='list')` – ashkangh Sep 09 '21 at 01:21
  • @ashkangh you mean the CSV? – Douglas Soares Sep 09 '21 at 01:23
  • No no! I mean when you read your csv data file, and you have your dataframe (whose name is df), you can use above command to print a 10 rows of your data, and you can add that data(which will be as a dictionary) to your question. you can use [this answer](https://stackoverflow.com/a/63163254/13131320) – ashkangh Sep 09 '21 at 01:31
  • Another thing, you should avoid using `for` loops in pandas since the ability of pandas is to enjoy vectorized calculations. In fact, in most cases, pandas has methods and functions that serve your purpose. – ashkangh Sep 09 '21 at 01:33

2 Answers2

0

One way you could do it is to just iterate over the rows and add it in that way:

for i in range(newdf.shape[0]):
   result = get(newdf.iloc[i,0])
   if result == 200:
      newdf.iloc[i,1] = "yes"

etc.

Chaos_Is_Harmony
  • 498
  • 5
  • 17
0

You might do it in one single line using apply:

newdf['login'] = newdf['Site'].apply(lambda x : "yes" if get(x).status_code == 200 else "no")
Sebastien D
  • 4,369
  • 4
  • 18
  • 46