Search code examples
pythonpandasloopsdataframecalculated-columns

Update column with data of FOR LOOP Pandas


I have the below code, I need that the output of the print update a new column.

import pandas as pd
import re
import numpy as np
import urllib.parse
from urllib.request import urlopen
import requests
from bs4 import BeautifulSoup
df = pd.read_csv('IR006.csv')
pd.set_option('display.max_colwidth', -1)

df4 = pd.read_csv('IR006.csv')
df4['UPDATE'] = "" **#This is the column where i wanna see the output of the for loop**

So, here is the loop that fetch data from URL:

for link in df4.iterrows():
    url = link[1]['URL'].replace('/v01/', '/depot/')
    x = urlopen(url)
    new = x.read()
    soup = BeautifulSoup(new, "lxml-xml")
    match = ''.join(re.findall(r"[C][L]\S{8}", str(soup)))
    print(match)

Output:

CLdbDQgFdD
CLYwHQYDVR

CLYwHQYDVR
CLYwHQYDVR

CLYwHQYDVR

The Dataframe look like this:

DATAFRAME

So how I can put the data that generates the loop in a new column name "UPDATE"


Solution

  • Try the following code:

    for idx,row in df4.iterrows():
        url = row['URL'].replace('/v01/', '/depot/')
        x = urlopen(url)
        new = x.read()
        soup = BeautifulSoup(new, "lxml-xml")
        match = ''.join(re.findall(r"[C][L]\S{8}", str(soup)))
        df4.at[idx,'UPDATE'] = match