您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:python 实现添加标签&打标签的操作

51自学网 2021-10-30 22:39:27
  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']=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)

使用函数来打标签

#标注标签 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)#打标签,判断天数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)

补充:python 根据标签名获取标签内容

看代码吧~

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持51zixue.net。


Python 如何让特征值滞后一行
python数据类型相关知识扩展
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。