当前位置: 首页 > news >正文

字符串函数5-9题(30 天 Pandas 挑战)

字符串函数

  • 1. 相关知识点
    • 1.5 字符串的长度条件判断
    • 1.6 apply映射操作
    • 1.7 python大小写转换
    • 1.8 正则表达式匹配
    • 2.9 包含字符串查询
  • 2. 题目
    • 2.5 无效的推文
    • 2.6 计算特殊奖金
    • 2.7 修复表中的名字
    • 2.8 查找拥有有效邮箱的用户
    • 2.9 患某种疾病的患者

1. 相关知识点

1.5 字符串的长度条件判断

tweets=tweets[tweets['content'].str.len()>15]

1.6 apply映射操作

  • 参数是一列(axis=0)或一行(axis=1)的数据
    # a为一行数据
    employees['bonus']=employees.apply(lambda a: a[2] if(a[0]%2==1 and a[1][0]!='M') else 0,axis=1)
    

1.7 python大小写转换

users['name'].map(lambda x: x[0].upper()+x[1:].lower())

1.8 正则表达式匹配

# index的值为True或False
index=users['mail'].str.contains("^[a-zA-Z][0-9a-zA-Z\\.\\_\\/\\-]*@leetcode.com$",regex=True)

2.9 包含字符串查询

patients[patients['conditions'].str.contains('DIAB1')]

2. 题目

2.5 无效的推文

在这里插入图片描述

在这里插入图片描述

import pandas as pddef invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame:return tweets[tweets['content'].str.len()>15][['tweet_id']]# return tweets[tweets['content'].apply(lambda x: True if len(x)>15 else False)][['tweet_id']]

2.6 计算特殊奖金

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import pandas as pddef calculate_special_bonus(employees: pd.DataFrame) -> pd.DataFrame:employees['bonus']=employees.apply(lambda a: a[2] if(a[0]%2==1 and a[1][0]!='M') else 0,axis=1)return employees[['employee_id','bonus']].sort_values('employee_id')

2.7 修复表中的名字

在这里插入图片描述
在这里插入图片描述

import pandas as pddef fix_names(users: pd.DataFrame) -> pd.DataFrame:users['name']=users['name'].map(lambda x: x[0].upper()+x[1:].lower())return users

2.8 查找拥有有效邮箱的用户

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import pandas as pddef valid_emails(users: pd.DataFrame) -> pd.DataFrame:return users[users['mail'].str.contains("^[a-zA-Z][0-9a-zA-Z\\.\\_\\/\\-]*@leetcode.com$",regex=True)]

2.9 患某种疾病的患者

在这里插入图片描述
在这里插入图片描述

import pandas as pddef find_patients(patients: pd.DataFrame) -> pd.DataFrame:return patients[patients['conditions'].str.contains('DIAB1')]
http://www.lryc.cn/news/392197.html

相关文章:

  • 【C语言题目】34.猜凶手
  • C++ 多进程多线程间通信
  • 怎么做防御系统IPS
  • 达梦数据库的系统视图v$auditrecords
  • Spring Boot与MyBatis-Plus:代码逆向生成指南
  • 【MySQL】mysql访问
  • (1)Jupyter Notebook 下载及安装
  • 监控平台zabbix对接grafana
  • 14-11 2024 年的 13 个 AI 趋势
  • 计算机大方向的选择
  • 使用Qt Installer Framework在centos7中打包
  • 您的私人办公室!-----ONLYOFFICE8.1版本的桌面编辑器测评
  • 点估计和参数分布的对比
  • 桌面保存的Word文件删除怎么找回?超实用的三个方法?
  • 【leetcode】双指针算法题
  • vue-router 源码分析——8.重定向
  • CAN总线协议
  • NLP篇1
  • 【一念发动便是行】念头,就是命运
  • Django + Vue 实现图片上传功能的全流程配置与详细操作指南
  • 【介绍下R-tree,什么是R-tree?】
  • 每天10个js面试题(二)
  • 深入理解【 String类】
  • Nacos 2.x 系列【20】集群部署
  • LeetCode刷题记录:(15)三角形最小路径和
  • 【大数据面试题】35 Spark 怎么做优化?
  • 2024年保安员职业资格考试题库大数据揭秘,冲刺高分!
  • 怎么搭建个人博客教程,附云主机选购指南
  • 使用Llama3/Qwen2等开源大模型,部署团队私有化Code Copilot和使用教程
  • C语言_结构体初阶(还未写完)