使用 FOR LOOP Pandas 的数据更新列

Update column with data of FOR LOOP Pandas

我有以下代码,我需要打印的输出更新一个新列。

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**

所以,这是从 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)

输出:

CLdbDQgFdD
CLYwHQYDVR

CLYwHQYDVR
CLYwHQYDVR

CLYwHQYDVR

Dataframe 看起来像这样:

那么我如何将生成循环的数据放入新的列名“UPDATE”

试试下面的代码:

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