python 實(shí)現(xiàn)添加標(biāo)簽&打標(biāo)簽的操作
odue_df=df_train_stmt.loc[(df_train_stmt.AGE3>0)|(df_train_stmt.AGE4>0)|(df_train_stmt.AGE5>0)|(df_train_stmt.AGE6>0),[’XACCOUNT’]].drop_duplicates()odue_df[’label’]=1cust_df=df_acct[[’CUSTR_NBR’,’XACCOUNT’]].drop_duplicates()#做合并df_y=pd.merge(cust_df,odue_df,how=’left’,on=’XACCOUNT’).groupby(’CUSTR_NBR’).agg({’label’:max}).reset_index().fillna(0)使用函數(shù)來(lái)打標(biāo)簽
#標(biāo)注標(biāo)簽 Labeldef label(row): if row[’Date_received’] == ’null’: return -1 if row[’Date’] != ’null’: td = pd.to_datetime(row[’Date’], format=’%Y%m%d’) - pd.to_datetime(row[’Date_received’], format=’%Y%m%d’) if td <= pd.Timedelta(15, ’D’): return 1 return 0dfoff[’label’] = dfoff.apply(label, axis=1)#打標(biāo)簽,判斷天數(shù)def get_label(s): s = s.split(’:’) if s[0]==’null’:return 0 elif (date(int(s[0][0:4]),int(s[0][4:6]),int(s[0][6:8]))-date(int(s[1][0:4]),int(s[1][4:6]),int(s[1][6:8]))).days<=15:return 1 else:return -1dataset2.label = dataset2.label.apply(get_label)
補(bǔ)充:python 根據(jù)標(biāo)簽名獲取標(biāo)簽內(nèi)容
看代碼吧~import reimport json import requestsfrom bs4 import BeautifulSoupimport lxml.htmlfrom lxml import etree result = requests.get(’http://example.webscraping.com/places/default/view/Algeria-4’)with open(’123.html’, ’wb’) as f: f.write(result.content)# print(parse_regex(result.text))test_data = '''<div> <ul> <li class='item-0'><a rel='external nofollow' rel='external nofollow' id='places_neighbours__row'>9,596,960first item</a></li> <li class='item-1'><a rel='external nofollow' >second item</a></li></ul> <book> <title lang='aaengbb'>Harry Potter</title> <price id='places_neighbours__row'>29.99</price> </book> <book><title lang='zh'>Learning XML</title><price>39.95</price> </book> <book><title>Python</title><price>40</price> </book> </div>'''# //div/ul/li/a[@id] 選取a標(biāo)簽中帶有id屬性的標(biāo)簽# //div/ul/li/a 選取所有a標(biāo)簽# //div/ul/li[2]/a'''/ 從根標(biāo)簽開始 必須具有嚴(yán)格的父子關(guān)系// 從當(dāng)前標(biāo)簽 后續(xù)節(jié)點(diǎn)含有即可選出* 通配符 選擇所有//div/book[1]/title 選擇div下第一個(gè)book標(biāo)簽的title標(biāo)簽//div/book[1]/tittle[@lang='zh'] 選擇div下第一個(gè)book標(biāo)簽的title標(biāo)簽并且內(nèi)容是zh的title標(biāo)簽//div/book/title //book/title //title 具有相同結(jié)果 只不過(guò)選取路徑不一樣//book/title/@* 將title所有的屬性值選出來(lái)//book/title/text() 將title的內(nèi)容選擇出來(lái),使用內(nèi)置函數(shù)//a[@href='http://www.4tl426be.cn/bcjs/link1.html' rel='external nofollow' rel='external nofollow' and @id='places_neighbours_row']//div/book/[last()]/title/text() 將最后一個(gè)book元素選出//div/book[price > 39]/title/text() 將book子標(biāo)簽price數(shù)值大于39的選擇出來(lái)//li[starts-with(@class,’item’)] 將class屬性前綴是item的選出來(lái)//title[contains(@lang,'eng')]將title屬性lang含有eng關(guān)鍵字的標(biāo)簽選出'''html = lxml.html.fromstring(test_data) # 加載任意一個(gè)字符串html_data = html.xpath(’//title[contains(@lang,'eng')]’) # xpath 查找路徑# print(dir(html_data[0])) # 查看html_data有什么功能print(html_data)for i in html_data:print(i.text)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問(wèn)題3. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼4. Python編寫nmap掃描工具5. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)6. 如何在jsp界面中插入圖片7. .NET6打包部署到Windows Service的全過(guò)程8. Ajax返回值類型與用法實(shí)例分析9. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)10. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法
