Pandas | 11 字符串函数
在本章中,我们将使用基本系列/索引来讨论字符串操作。在随后的章节中,将学习如何将这些字符串函数应用于数据帧(DataFrame)。
Pandas提供了一组字符串函数,可以方便地对字符串数据进行操作。 最重要的是,这些函数忽略(或排除)丢失/NaN值。
几乎这些方法都使用Python字符串函数(请参阅: http://docs.python.org/3/library/stdtypes.html#string-methods )。 因此,将Series对象转换为String对象,然后执行该操作。
下面来看看每个操作的执行和说明。
| 编号 | 函数 | 描述 |
|---|---|---|
| 1 | lower() |
将Series/Index中的字符串转换为小写。 |
| 2 | upper() |
将Series/Index中的字符串转换为大写。 |
| 3 | len() |
计算字符串长度。 |
| 4 | strip() |
帮助从两侧的系列/索引中的每个字符串中删除空格(包括换行符)。 |
| 5 | split(' ') |
用给定的模式拆分每个字符串。 |
| 6 | cat(sep=' ') |
使用给定的分隔符连接系列/索引元素。 |
| 7 | get_dummies() |
返回具有单热编码值的数据帧(DataFrame)。 |
| 8 | contains(pattern) |
如果元素中包含子字符串,则返回每个元素的布尔值True,否则为False。 |
| 9 | replace(a,b) |
将值a替换为值b。 |
| 10 | repeat(value) |
重复每个元素指定的次数。 |
| 11 | count(pattern) |
返回模式中每个元素的出现总数。 |
| 12 | startswith(pattern) |
如果系列/索引中的元素以模式开始,则返回true。 |
| 13 | endswith(pattern) |
如果系列/索引中的元素以模式结束,则返回true。 |
| 14 | find(pattern) |
返回模式第一次出现的位置。 |
| 15 | findall(pattern) |
返回模式的所有出现的列表。 |
| 16 | swapcase |
变换字母大小写。 |
| 17 | islower() |
检查系列/索引中每个字符串中的所有字符是否小写,返回布尔值 |
| 18 | isupper() |
检查系列/索引中每个字符串中的所有字符是否大写,返回布尔值 |
| 19 | isnumeric() |
检查系列/索引中每个字符串中的所有字符是否为数字,返回布尔值。 |
现在创建一个系列,看看上述所有函数是如何工作的。
import pandas as pd
import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '','SteveMinsu'])
print (s)
输出结果:
0 Tom
1 William Rick
2 John
3 Alber@t
4 NaN
5 1234
6 SteveMinsu
dtype: object
1. lower()函数示例
import pandas as pd
import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '','SteveMinsu'])
print (s.str.lower())
输出结果:
0 tom
1 william rick
2 john
3 alber@t
4 NaN
5 1234
6 steveminsu
dtype: object
2. upper()函数示例
import pandas as pd
import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '','SteveMinsu'])
print (s.str.upper())
输出结果:
0 TOM
1 WILLIAM RICK
2 JOHN
3 ALBER@T
4 NaN
5 1234
6 STEVESMITH
dtype: object
3. len()函数示例
import pandas as pd
import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '','SteveMinsu'])
print (s.str.len())
输出结果:
0 3.0
1 12.0
2 4.0
3 7.0
4 NaN
5 4.0
6 10.0
dtype: float64
4. strip()函数示例
import pandas as pd
import numpy as np
s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print (s)
print('\n') print ("=========== After Stripping ================")
print (s.str.strip())
输出结果:
0 Tom
1 William Rick
2 John
3 Alber@t
dtype: object
=========== After Stripping ================
0 Tom
1 William Rick
2 John
3 Alber@t
dtype: object
5. split(pattern)函数示例
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print (s)
print('\n') print ("================= Split Pattern: ==================")
print (s.str.split(' '))
输出结果:
0 Tom
1 William Rick
2 John
3 Alber@t
dtype: object
================= Split Pattern: ==================
0 [Tom, ]
1 [, William, Rick]
2 [John]
3 [Alber@t]
dtype: object
6. cat(sep=pattern)函数示例
查看时候的分隔符
import pandas as pd
import numpy as np s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print (s.str.cat(sep=' <=> '))
输出结果:
Tom <=> William Rick <=> John <=> Alber@t
7. get_dummies()函数示例
import pandas as pd
import numpy as np s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print (s.str.get_dummies())
输出结果:
William Rick Alber@t John Tom
0 0 0 0 1
1 1 0 0 0
2 0 0 1 0
3 0 1 0 0
8. contains()函数示例
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print (s.str.contains(' '))
输出结果:
0 True
1 True
2 False
3 False
dtype: bool
9. replace(a,b)函数示例
import pandas as pd
s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print (s)
print('\n') print ("After replacing @ with $: ============== ")
print (s.str.replace('@','$'))
输出结果:
0 Tom
1 William Rick
2 John
3 Alber@t
dtype: object
After replacing @ with $: ==============
0 Tom
1 William Rick
2 John
3 Alber$t
dtype: object
10. repeat(value)函数示例
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print (s.str.repeat(2))
输出结果:
0 Tom Tom
1 William Rick William Rick
2 JohnJohn
3 Alber@tAlber@t
dtype: object
11. count(pattern)函数示例
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print ("The number of 'm's in each string:")
print (s.str.count('m'))
输出结果:
The number of 'm's in each string:
0 1
1 1
2 0
3 0
dtype: int64
12. startswith(pattern)函数示例
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print ("Strings that start with 'T':")
print (s.str. startswith ('T'))
输出结果:
Strings that start with 'T':
0 True
1 False
2 False
3 False
dtype: bool
13. endswith(pattern)函数示例
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print ("Strings that end with 't':")
print (s.str.endswith('t'))
输出结果:
Strings that end with 't':
0 False
1 False
2 False
3 True
dtype: bool
14. find(pattern)函数示例
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print (s.str.find('e'))
输出结果:
0 -1
1 -1
2 -1
3 3
dtype: int64
注意:
-1表示元素中没有这样的模式可用。
15. findall(pattern)函数示例
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t'])
print (s.str.findall('e'))
输出结果:
0 []
1 []
2 []
3 [e]
dtype: object
空列表(
[])表示元素中没有这样的模式可用。
16. swapcase()函数示例
import pandas as pd s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t'])
print (s.str.swapcase())
输出结果:
0 tOM
1 wILLIAM rICK
2 jOHN
3 aLBER@T
dtype: object
17. islower()函数示例
import pandas as pd s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t'])
print (s.str.islower())
输出结果:
0 False
1 False
2 False
3 False
dtype: bool
18. isupper()函数示例
import pandas as pd s = pd.Series(['TOM', 'William Rick', 'John', 'Alber@t'])
print (s.str.isupper())
输出结果:
0 True
1 False
2 False
3 False
dtype: bool
19. isnumeric()函数示例
import pandas as pd s = pd.Series(['Tom', '','William Rick', 'John', 'Alber@t'])
print (s.str.isnumeric())
输出结果:
0 False
1 True
2 False
3 False
4 False
dtype: bool
Pandas | 11 字符串函数的更多相关文章
- python pandas字符串函数详解(转)
pandas字符串函数详解(转)——原文连接见文章末尾 在使用pandas框架的DataFrame的过程中,如果需要处理一些字符串的特性,例如判断某列是否包含一些关键字,某列的字符长度是否小于3等等 ...
- 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---3
字符串函数总结: 下面是头文件 string.h 中定义的函数: 序号 函数 & 描述 1 void *memchr(const void *str, int c, size_t n)在参数 ...
- 11、numpy——字符串函数
NumPy 字符串函数 以下函数用于对 dtype 为 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作. 它们基于 Python 内置库中的标准字符串函数. ...
- 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---2
11-3:字符串 输出 三个标准的函数:puts() fputs() printf() 1.puts()函数: #include<stdio.h> #define DEF "I ...
- 利用Python进行数据分析(15) pandas基础: 字符串操作
字符串对象方法 split()方法拆分字符串: strip()方法去掉空白符和换行符: split()结合strip()使用: "+"符号可以将多个字符串连接起来: join( ...
- SQLSERVER常见系统函数之字符串函数(一)
好久没有写博客了,这段时间准备写一下字符串函数 QQ群: 499092562:欢迎交流 字符串函数: 1.LEN(需要获取长度的字符串) 返回:字符串的长度 示例: SELECT LEN('小搬运工很 ...
- Sql Server函数全解<一>字符串函数
阅读目录 1.ASCII()函数 2.CHAR()函数 3.LEFT()函数 4.RIGHT()函数 5.LTRIM()函数 6.RTRIM()函数 7.STR()函数 8.字符串逆序的函数REVER ...
- Sql Server系列:字符串函数
字符串函数用于对字符和二进制字符串进行各种操作,大多数字符串函数只能作用于char.nchar.varchar和nvarchar数据类型.字符串函数可以用在SELECT或者WHERE语句中. 1. A ...
- 前端学PHP之字符串函数
× 目录 [1]特点 [2]输出 [3]空格[4]大小写[5]HTML[6]格式化[7]比较 前面的话 字符串的处理和分析在任何编程语言中都是一个重要的基础,往往是简单而重要的.信息的分类.解析.存储 ...
随机推荐
- C#:ref关键字和out关键字的区别
1.在不使用关键字(比如ref关键字.out关键字等)修饰函数的情况下,大部分函数的参数是以值传递的方式,也就是说,“调用函数”在使用参数(比如myNumber)时,是把该参数复制多一份,然后将其传递 ...
- 前端开发vscode必备插件
VSCode 插件 Atom one Dark Theme Atom Dark主题 Auto Close Tag 自动关闭标签 Auto Rename Tag 自动重命名标签 Beautify 格式化 ...
- Windows安装Nginx需要注意的地方
在使用 Nginx 之前,首先要三连问,它是什么?用来做什么?为什么用它? 这篇文章很好的解答了上面的问题,并补充了什么是正向代理和反向代理以及区别的知识 https://www.cnblogs. ...
- RandomAccessFile vs FileChannel.open(path);
What kind of FileChannel object does the FileChannel.open(path) method return? Is it still random ac ...
- fancybit个人简介
程序员一枚 熟悉C C++ C# js lua等多种常见开发语言 熟悉Unity游戏开发 node.js pomelo和C# scut 网游后端框架 做过.net和php网站后端 二次元文化爱好者 有 ...
- 【翻译】Tusdotnet中文文档(3)自定义功能和相关技术
自定义功能和相关技术 本篇按照如下结构翻译 自定义功能 自定义数据仓库 相关技术 架构和总体概念 自定义数据仓库 tusdotnet附带一个存储库TusDiskStore,它将文件保存在磁盘上的一个目 ...
- DISK2VHD 转win2008 无法启动
windows 2008R2物理机,使用微软的DISK2VHD转换成虚拟盘,挂到虚拟机上,无法启动只有光标闪.找来window2008安装盘 选择“修复windows系统”, 调出cmd命令提示符Bo ...
- 字节输出流FileOutputStream
#字节流 字节输出流FileOutputStream 创建输出流对象 OutputStream 流对象是一个抽象类,不能实例化.所以,我们要找一个具体的子类 :FileOutputStream. 查看 ...
- loj#10172 涂抹果酱 (状压DP)
题目: #10172. 「一本通 5.4 练习 1」涂抹果酱 解析: 三进制的状压DP 经过简单的打表发现,在\(m=5\)时最多有\(48\)种合法状态 然后就向二进制一样枚举当前状态和上一层的状态 ...
- 实验吧——看起来有点难(sql盲注)
题目地址:http://ctf5.shiyanbar.com/basic/inject/ 首先当然是拿admin/admin来试试啊,多次测试发现,有两种错误提示 1.数据库连接失败! 2.登录失败, ...