Python: using loop to add new column to dataframe -


i have column dataframe

url     http://www.utt66.ru/cat/13972/ https://yandex.ru/yandsearch?clid=2186618&text=%d0%ba%d0%b0%d0%bb%d1%8c%d1%8f%d0%bd%d1%8b%20khalil%20mamoon%20%d0%ba%d1%83%d0%bf%d0%b8%d1%82%d1%8c%20 http://yandex.ru/clck/jsredir?from=yandex.ru%3byandsearch%3bweb%3b%3b&text=&etext=1086.0aueawftxcruqs1zi5nayk9j2mh6yse_nxu9lq-mwhfjitiqsm3jlm3ks26cautme7x0u6hng06uugra-9hhsq.b9a03b0331f4f4f46de76151fcec7f63060561ea&uuid=&state=zrrsea3ay6h77jydlxkyipmtcidgyjuu&data=ulnrnmk5wktyejr0ewjfyk1ldmtxdxhxmmr3bgz5cw51slvkblgwatezlxhaskrmbda2btlsyulps3broehmawnznlnbr2zsdzq2rwvurdk3mlfiylrzrmxvzwzry3nzlxvundl5wm1gtljqy2l3dhvlszndyms5m2hwnlztulrqcxbpc1jaczb3wkdleuc3ykncyuxkcdl2akdtttbwtvzxz0doz0cxrm94ukfywhpuujlnrkj0bvhky2jqbzdfwlbuejfwtlhtzezhnw13ze5uac11rg1pdhhnn0rlshetakgtvflvtwgta2jyaer0ugz0ugxpu2vhatvmntvrn05lmfrrbmrzcc1fd3lqegdnb19xdgjyv1b4y1bdz3dvtmfhsf9jeldtv084zjfhzfnorkjfv3zmouo1wthicldxtwxelw1lewltbkdxsersa2fgz2exa2nlsv9jefaycq&b64e=2&sign=6b914765fe78a3e68831c2b6828cf669&keyno=0&cst=aiuy0dbwfj4bwm_uhlttxgxxuvzkw7y-ntwzo3btfifl2v7d7v1qvnh8sezecucbuxmsm6evbji9wdnuntadiecfvaczaypi8lv4snxdamz8q5ps4fuuccaf_tr0uwfikc6ag-nx6lb2wca1pp9l6qse3tipcwsrge_jueau4dtg3rwqcdxvk1v6q6p9qr8g8exclnwinu9ei2wqgeafurn9z_2zupktyzvucqzkm0iacmpofvg3mjuy6emscb54roqiyzcqa3o4eohhr0h2ygb66j-005u2&ref=orjy4mgprjk5bodnw0uvlpagqs5jg3quklfgkhgczzlbh-w_ninsoy4el2-ciphypxll_ot04hcjajobtz07dhig7d1ehiq2tebi8rcsnoahkmgoosziejhtftvm7arlnyufk3cqiaug6tvqqcr2ki_xyek72uaw_aawnfmwbzprptchlirbwodvzexiksvo&l10n=ru&cts=1466022252275&mc=4.058813890331201 https://www.google.ru/search?ie=utf-8&hl=ru&q=%d0%ba%d0%b0%d0%bb%d1%8c%d1%8f%d0%bd%d1%8b%20khalil%20mamoon%20%d0%ba%d1%83%d0%bf%d0%b8%d1%82%d1%8c&gws_rd=ssl https://vk.com/im?peers=52525981_172627017_275902975_339455414_c107&sel=203575078 https://www.google.ru/search?ie=utf-8&hl=ru&q=%d0%ba%d0%b0%d0%bb%d1%8c%d1%8f%d0%bd%d1%8b%20khalil%20mamoon%20%d0%ba%d1%83%d0%bf%d0%b8%d1%82%d1%8c&gws_rd=ssl http://www.utt66.ru/cat/13972/ http://www.utt66.ru/contacts/ https://vk.com/im?peers=52525981_172627017_203575078_275902975_339455414&sel=c107 http://www.utt66.ru/feedback/ 

i need request url, contain yandex.ru/yandsearch , google.ru/search , use code

    if '//www.google.ru/search?' not in urls[i] , '//www.google.ru/search?' in urls[i - 1]:         get1 = urlparse(urls[i - 1])         dict1 = parse_qs(get1[4])         search_val = dict1['q'][0]         searching_val.append(search_val)     elif '//yandex.ru/yandsearch?' not in urls[i] , '//yandex.ru/yandsearch?' in urls[i - 1]:         get2 = urlparse(urls[i - 1])         dict2 = parse_qs(get2[4])         search_val = dict2['text'][0]         searching_val.append(search_val) 

i want, if find string, in next string write request. after df['search_term'] = searching_val uncorrect answer. wrong?

in loop, did not assign any value when none of addresses matched. add else statement , add either empty string or null - number of elements in searching_val must equal number of rows in dataframe (or url column, if wish).

also, convert searching_val pandas.series , assign column:

import pandas pd  df['search_term'] = pd.series(searching_val) 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -