pandas(十二)的字符串的处理20种方法 您所在的位置:网站首页 提取字符串中的指定文字 pandas(十二)的字符串的处理20种方法

pandas(十二)的字符串的处理20种方法

2023-07-08 07:35| 来源: 网络整理| 查看: 265

pandas(十二)的字符串的处理20种方法 原创

风华浪浪 2023-07-08 06:44:51 博主文章分类:pandas ©著作权

文章标签 pandas mysql 数据库 字符串 正则表达式 文章分类 HarmonyOS 后端开发

©著作权归作者所有:来自51CTO博客作者风华浪浪的原创作品,请联系作者获取转载授权,否则将追究法律责任

一、Pandas 中的 Series 对象的各种字符串方法

方法

描述

Series.str.capitalize()

将Series中的字符串转换为首字母大写的形式

Series.str.casefold()

将Series中的字符串转换为小写形式

Series.str.cat()

使用给定的分隔符连接Series中的字符串

Series.str.center()

在Series中的字符串两侧填充空格,使其居中显示

Series.str.contains()

检查Series中的字符串是否包含指定的模式或正则表达式

Series.str.count()

统计Series中的每个字符串中指定模式的出现次数

Series.str.endswith()

检查Series中的字符串是否以指定的模式结尾

Series.str.extract()

从Series中的字符串中提取正则表达式中的捕获组作为DataFrame的列

Series.str.find()

返回Series中每个字符串中指定子字符串的最低索引

Series.str.join()

使用指定的分隔符将Series中作为元素的列表连接起来

Series.str.len()

计算Series中每个字符串的长度

Series.str.lower()

将Series中的字符串转换为小写形式

Series.str.lstrip()

移除Series中每个字符串的左侧空白字符

Series.str.match()

检查Series中的每个字符串是否以正则表达式匹配

Series.str.replace()

将Series中的字符串中的指定模式或正则表达式替换为指定的值

Series.str.rstrip()

移除Series中每个字符串的右侧空白字符

Series.str.split()

使用指定的分隔符将Series中的字符串分割成子字符串

Series.str.startswith()

检查Series中的字符串是否以指定的模式开头

Series.str.strip()

移除Series中每个字符串的左侧和右侧空白字符

Series.str.upper()

将Series中的字符串转换为大写形式

二、Pandas字符串处理

前面我们已经使用了字符串的处理函数:df["bWendu"7.str.replace("C", m).astype (int32')

Pandas的字符串处理

使用方法:先获取Series的str属性,然后在属性上调用函数;只能在字符串列上使用,不能数字列上使用;Dataframe 上没有str属性和处理方法Series.str并不是Python原生字符串,而是自己的一套方法,不过大部分和原生str很相似;

本节演示内容:

获取Series的str属性,然后使用各种字符串处理函数使用str的startswith、contains等boo!类Series可以做条件查询需要多次str处理的链式操作使用正则表达式的处理:str默认支持正则表达式操作三、简单的示例import pandas as pd fpath = '/Users/python/Desktop/means/ml-25m/beijing_tianqi_2018.csv' df = pd.read_csv(fpath) ymd bWendu yWendu tianqi fengxiang fengli aqi aqiInfo aqiLevel 0 2018-01-01 3℃ -6℃ 晴~多云 东北风 1-2级 59 良 2 1 2018-01-02 2℃ -5℃ 阴~多云 东北风 1-2级 49 优 1 2 2018-01-03 2℃ -5℃ 多云 北风 1-2级 28 优 1 3 2018-01-04 0℃ -8℃ 阴 东北风 1-2级 28 优 1 4 2018-01-05 3℃ -6℃ 多云~晴 西北风 1-2级 50 优适用Series的str属性,适用各种字符串处理函数df['bWendu'].str.replace('℃', '') 将字符串替换成数字 0 3 1 2 .. 363 -2 364 -2df['bWendu'].str.isnumeric() 判读值是否为数字 number 类型 df['bWendu'].str.len() 判读str值的长度 0 False 1 False ... 363 False 364 False适用str的startswith、contains等得到bool的Serise做条件查询查询是否为2018年3月份的的天气 condition = df['ymd'].str.startswith('2018-03') print(condition) 0 False 1 False ... 363 False 364 False df[condition].head() 查询2018年3月份的天气情况 ymd bWendu yWendu tianqi fengxiang fengli aqi aqiInfo aqiLevel 59 2018-03-01 8℃ -3℃ 多云 西南风 1-2级 46 优 1 60 2018-03-02 9℃ -1℃ 晴~多云 北风 1-2级 95 良 2 61 2018-03-03 13℃ 3℃ 多云~阴 北风 1-2级 214 重度污染 5 62 2018-03-04 7℃ -2℃ 阴~多云 东南风 1-2级 144 轻度污染 3 63 2018-03-05 8℃ -3℃ 晴 南风 1-2级 94 良 2需要多次str处理的链式操作 怎样提取201803这样的数字月份 先将日期2018-03-31替换成20180331的形式提取月份字符串201803df['ymd'].str.replace('-', '').str.slice(0, 6)df.loc[:, 'ymd'] = df['ymd'].str.replace('-', '') condition = df['ymd'].str.startswith('201803') df[condition].head()将温度去掉℃并且转换成int类型 df.loc[:, 'bWendu'] = df['bWendu'].str.replace('℃', '').astype('int32') df.loc[:, 'yWendu'] = df['yWendu'].str.replace('℃', '').astype('int32')适用正则表达式的处理添加新的一列,添加中文 年月日 def get_nianyueri(x): year, month, day = x['ymd'].split('-') return f"{year}年{month}月{day}" df['日期'] = df.apply(get_nianyueri, axis=1) df['日期'] 0 2018年01月01 1 2018年01月02 ... 363 2018年12月30 364 2018年12月31如何去掉年月日中文字符去掉 方式一: df['日期'].str.replace('年', '').str.replace('月', '').str.replace('日', '') 方式二:Series.str 默认开启正则表达式模式 只要遇到年月日三个字符串的其中一个均替换,且替换成功 df['日期'].str.replace('[年月日]', '') 0 20180101 1 20180102 ... 363 20181230 364 20181231

收藏 评论 分享 举报

上一篇:Linux之curl 风骚用法

下一篇:pandas(三)数据查询



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有