Python添加标签具体怎样实现,代码是什么
Admin 2022-07-29 群英技术资讯 1040 次浏览
今天这篇给大家分享的知识是“Python添加标签具体怎样实现,代码是什么”,小编觉得挺不错的,对大家学习或是工作可能会有所帮助,对此分享发大家做个参考,希望这篇“Python添加标签具体怎样实现,代码是什么”文章能帮助大家解决问题。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']=1
cust_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)
#标注标签 Label
def 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 0
dfoff['label'] = dfoff.apply(label, axis=1)
#打标签,判断天数
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 -1
dataset2.label = dataset2.label.apply(get_label)
补充:python 根据标签名获取标签内容
import re
import json
import requests
from bs4 import BeautifulSoup
import lxml.html
from 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 href="link1.html" rel="external nofollow" rel="external nofollow" id="places_neighbours__row">9,596,960first item</a></li>
<li class="item-1"><a href="link2.html" rel="external nofollow" >second item</a></li>
<li class="item-inactive"><a href="link3.html" rel="external nofollow" >third item</a></li>
<li class="item-1"><a href="link4.html" rel="external nofollow" id="places_neighbours__row">fourth item</a></li>
<li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" >fifth item</a></li>
<li class="good-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" >fifth 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标签中带有id属性的标签
# //div/ul/li/a 选取所有a标签
# //div/ul/li[2]/a
"""
/ 从根标签开始 必须具有严格的父子关系
// 从当前标签 后续节点含有即可选出
* 通配符 选择所有
//div/book[1]/title 选择div下第一个book标签的title标签
//div/book[1]/tittle[@lang="zh"] 选择div下第一个book标签的title标签并且内容是zh的title标签
//div/book/title //book/title //title 具有相同结果 只不过选取路径不一样
//book/title/@* 将title所有的属性值选出来
//book/title/text() 将title的内容选择出来,使用内置函数
//a[@href="link1.html" rel="external nofollow" rel="external nofollow" and @id="places_neighbours_row"]
//div/book/[last()]/title/text() 将最后一个book元素选出
//div/book[price > 39]/title/text() 将book子标签price数值大于39的选择出来
//li[starts-with(@class,'item')] 将class属性前缀是item的选出来
//title[contains(@lang,"eng")]将title属性lang含有eng关键字的标签选出
"""
html = lxml.html.fromstring(test_data) # 加载任意一个字符串
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)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在flask更新到1.0之后的版本,官方推荐使用flask run的方式运行程序,可是作为开发,如果没有了pycharm的断点调试,这可太难受了。 所以,本篇博客主要讲述如何在pycharm中运行flask程序,并开启Debug模式。
这篇文章主要给大家介绍了关于Python使用scipy进行曲线拟合的相关资料,Scipy优化和拟合采用的是optimize模块,该模块提供了函数最小值(标量或多维)、曲线拟合和寻找等式的根的有用算法,需要的朋友可以参考下
我们在使用Python的过程中,会遇到这种情况:需要将接收的bytes数据转换为整形数,或者是将整形数转换成bytes数据。之前小编介绍过在Python中可以转换为整形数的int函数。本文小编就介绍Python中int与bytes如何相互转换的过程:int.to_bytes()和int.from_bytes()。
这篇文章主要介绍了pandas读取excel时获取读取进度的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍了Python PIL图片如何按比例裁剪,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008