简体   繁体   English

数据帧中的Python for循环

[英]Python for loop in dataframe

Good day, guys!美好的一天,伙计们! Be carefule of the COVID19 and solve 1 problem plz.请小心 COVID19 并解决 1 个问题。

I really want to get the address through crawling below with the value of column B and put it in column F. → This is problem what I got我真的很想通过下面的爬行得到地址 B 列的值并将其放在 F 列中。 → 这是我得到的问题

here's my source.这是我的消息来源。 (This is the source for making simple dataframes.) (这是制作简单数据框的来源。)

Start_Page = 0
End_Page = 500

for i in range(Start_Page, End_Page):
    page = i + 1
    url = "http://www.example.com &start=" + str(page)
    res = urllib.request.urlopen(url)
    json_str = response.read().decode("utf-8")
    json_object = json.loads(json_str)
    Mark = pd.json_normalize(json_object['jobs']['job'])
    df = pd.DataFrame(Mark)
    df = df[['A', 'B', 'C', 'D', 'E']]

column's name is A to E.列的名称是 A 到 E。

I really want to get the address through crawling below with the value of column B and put it in column F.我真的很想通过下面的爬行得到B列的值的地址,放到F列。

(This is the source for crawling.) (这是爬行的来源。)

B = B     # B in dataframe 'df'
result = urlopen("http://www.example2.com" + B)
html = result.read()
soup = BeautifulSoup(html, 'html.parser')

address = str(soup.select_one("address.txt_address > span"))
address = re.sub('<.+?>', '', address, 0).strip()
B = B2 → append address to F2
...
...
...
B = B110000 → append address to F110000

How do I create a 'for loop' that adds the address to column F by using the value of B as a variable?如何通过使用 B 的值作为变量来创建将地址添加到 F 列的“for 循环”? (I've been looking for a way for a week, but it was too difficult to find.) (我找了一个星期的方法,但是太难找了。)

I'm not sure if I understood the question right,我不确定我是否理解正确的问题,

but if your trying to get some address using the values from但是如果您尝试使用来自的值获取一些地址

'B' column and save it to a new 'F' column, 'B' 列并将其保存到新的 'F' 列,

you could just select 'B' column and put it in a for loop.您可以选择“B”列并将其放入for循环中。

values_for_f = []
for i in df['b'].values:
    result = urlopen("http://www.example2.com" + i)
    ....
    address = re.sub('<.+?>', '', address, 0).strip()
    values_for_f.append(address)
df['f'] = values_for_f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM