简介

在1.0之前,只有一种形式来存储text数据,那就是object。在1.0之后,添加了一个新的数据类型叫做StringDtype 。今天将会给大家讲解Pandas中text中的那些事。

创建text的DF

先看下常见的使用text来构建DF的例子:

In [1]: pd.Series(['a', 'b', 'c'])
Out[1]:
0 a
1 b
2 c
dtype: object

如果要使用新的StringDtype,可以这样:

In [2]: pd.Series(['a', 'b', 'c'], dtype="string")
Out[2]:
0 a
1 b
2 c
dtype: string In [3]: pd.Series(['a', 'b', 'c'], dtype=pd.StringDtype())
Out[3]:
0 a
1 b
2 c
dtype: string

或者使用astype进行转换:

In [4]: s = pd.Series(['a', 'b', 'c'])

In [5]: s
Out[5]:
0 a
1 b
2 c
dtype: object In [6]: s.astype("string")
Out[6]:
0 a
1 b
2 c
dtype: string

String 的方法

String可以转换成大写,小写和统计它的长度:

In [24]: s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'],
....: dtype="string")
....: In [25]: s.str.lower()
Out[25]:
0 a
1 b
2 c
3 aaba
4 baca
5 <NA>
6 caba
7 dog
8 cat
dtype: string In [26]: s.str.upper()
Out[26]:
0 A
1 B
2 C
3 AABA
4 BACA
5 <NA>
6 CABA
7 DOG
8 CAT
dtype: string In [27]: s.str.len()
Out[27]:
0 1
1 1
2 1
3 4
4 4
5 <NA>
6 4
7 3
8 3
dtype: Int64

还可以进行trip操作:

In [28]: idx = pd.Index([' jack', 'jill ', ' jesse ', 'frank'])

In [29]: idx.str.strip()
Out[29]: Index(['jack', 'jill', 'jesse', 'frank'], dtype='object') In [30]: idx.str.lstrip()
Out[30]: Index(['jack', 'jill ', 'jesse ', 'frank'], dtype='object') In [31]: idx.str.rstrip()
Out[31]: Index([' jack', 'jill', ' jesse', 'frank'], dtype='object')

columns的String操作

因为columns是String表示的,所以可以按照普通的String方式来操作columns:

In [34]: df.columns.str.strip()
Out[34]: Index(['Column A', 'Column B'], dtype='object') In [35]: df.columns.str.lower()
Out[35]: Index([' column a ', ' column b '], dtype='object')
In [32]: df = pd.DataFrame(np.random.randn(3, 2),
....: columns=[' Column A ', ' Column B '], index=range(3))
....: In [33]: df
Out[33]:
Column A Column B
0 0.469112 -0.282863
1 -1.509059 -1.135632
2 1.212112 -0.173215

分割和替换String

Split可以将一个String切分成一个数组。

In [38]: s2 = pd.Series(['a_b_c', 'c_d_e', np.nan, 'f_g_h'], dtype="string")

In [39]: s2.str.split('_')
Out[39]:
0 [a, b, c]
1 [c, d, e]
2 <NA>
3 [f, g, h]
dtype: object

要想访问split之后数组中的字符,可以这样:

In [40]: s2.str.split('_').str.get(1)
Out[40]:
0 b
1 d
2 <NA>
3 g
dtype: object In [41]: s2.str.split('_').str[1]
Out[41]:
0 b
1 d
2 <NA>
3 g
dtype: object

使用 expand=True 可以 将split过后的数组 扩展成为多列:

In [42]: s2.str.split('_', expand=True)
Out[42]:
0 1 2
0 a b c
1 c d e
2 <NA> <NA> <NA>
3 f g h

可以指定分割列的个数:

In [43]: s2.str.split('_', expand=True, n=1)
Out[43]:
0 1
0 a b_c
1 c d_e
2 <NA> <NA>
3 f g_h

replace用来进行字符的替换,在替换过程中还可以使用正则表达式:

s3.str.replace('^.a|dog', 'XX-XX ', case=False)

String的连接

使用cat 可以连接 String:

In [64]: s = pd.Series(['a', 'b', 'c', 'd'], dtype="string")

In [65]: s.str.cat(sep=',')
Out[65]: 'a,b,c,d'

使用 .str来index

pd.Series会返回一个Series,如果Series中是字符串的话,可通过index来访问列的字符,举个例子:

In [99]: s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan,
....: 'CABA', 'dog', 'cat'],
....: dtype="string")
....: In [100]: s.str[0]
Out[100]:
0 A
1 B
2 C
3 A
4 B
5 <NA>
6 C
7 d
8 c
dtype: string In [101]: s.str[1]
Out[101]:
0 <NA>
1 <NA>
2 <NA>
3 a
4 a
5 <NA>
6 A
7 o
8 a
dtype: string

extract

Extract用来从String中解压数据,它接收一个 expand参数,在0.23版本之前, 这个参数默认是False。如果是false,extract会返回Series,index或者DF 。如果expand=true,那么会返回DF。0.23版本之后,默认是true。

extract通常是和正则表达式一起使用的。

In [102]: pd.Series(['a1', 'b2', 'c3'],
.....: dtype="string").str.extract(r'([ab])(\d)', expand=False)
.....:
Out[102]:
0 1
0 a 1
1 b 2
2 <NA> <NA>

上面的例子将Series中的每一字符串都按照正则表达式来进行分解。前面一部分是字符,后面一部分是数字。

注意,只有正则表达式中group的数据才会被extract .

下面的就只会extract数字:

In [106]: pd.Series(['a1', 'b2', 'c3'],
.....: dtype="string").str.extract(r'[ab](\d)', expand=False)
.....:
Out[106]:
0 1
1 2
2 <NA>
dtype: string

还可以指定列的名字如下:

In [103]: pd.Series(['a1', 'b2', 'c3'],
.....: dtype="string").str.extract(r'(?P<letter>[ab])(?P<digit>\d)',
.....: expand=False)
.....:
Out[103]:
letter digit
0 a 1
1 b 2
2 <NA> <NA>

extractall

和extract相似的还有extractall,不同的是extract只会匹配第一次,而extractall会做所有的匹配,举个例子:

In [112]: s = pd.Series(["a1a2", "b1", "c1"], index=["A", "B", "C"],
.....: dtype="string")
.....: In [113]: s
Out[113]:
A a1a2
B b1
C c1
dtype: string In [114]: two_groups = '(?P<letter>[a-z])(?P<digit>[0-9])' In [115]: s.str.extract(two_groups, expand=True)
Out[115]:
letter digit
A a 1
B b 1
C c 1

extract匹配到a1之后就不会继续了。

In [116]: s.str.extractall(two_groups)
Out[116]:
letter digit
match
A 0 a 1
1 a 2
B 0 b 1
C 0 c 1

extractall匹配了a1之后还会匹配a2。

contains 和 match

contains 和 match用来测试DF中是否含有特定的数据:

In [127]: pd.Series(['1', '2', '3a', '3b', '03c', '4dx'],
.....: dtype="string").str.contains(pattern)
.....:
Out[127]:
0 False
1 False
2 True
3 True
4 True
5 True
dtype: boolean
In [128]: pd.Series(['1', '2', '3a', '3b', '03c', '4dx'],
.....: dtype="string").str.match(pattern)
.....:
Out[128]:
0 False
1 False
2 True
3 True
4 False
5 True
dtype: boolean
In [129]: pd.Series(['1', '2', '3a', '3b', '03c', '4dx'],
.....: dtype="string").str.fullmatch(pattern)
.....:
Out[129]:
0 False
1 False
2 True
3 True
4 False
5 False
dtype: boolean

String方法总结

最后总结一下String的方法:

Method Description
cat() Concatenate strings
split() Split strings on delimiter
rsplit() Split strings on delimiter working from the end of the string
get() Index into each element (retrieve i-th element)
join() Join strings in each element of the Series with passed separator
get_dummies() Split strings on the delimiter returning DataFrame of dummy variables
contains() Return boolean array if each string contains pattern/regex
replace() Replace occurrences of pattern/regex/string with some other string or the return value of a callable given the occurrence
repeat() Duplicate values (s.str.repeat(3) equivalent to x * 3)
pad() Add whitespace to left, right, or both sides of strings
center() Equivalent to str.center
ljust() Equivalent to str.ljust
rjust() Equivalent to str.rjust
zfill() Equivalent to str.zfill
wrap() Split long strings into lines with length less than a given width
slice() Slice each string in the Series
slice_replace() Replace slice in each string with passed value
count() Count occurrences of pattern
startswith() Equivalent to str.startswith(pat) for each element
endswith() Equivalent to str.endswith(pat) for each element
findall() Compute list of all occurrences of pattern/regex for each string
match() Call re.match on each element, returning matched groups as list
extract() Call re.search on each element, returning DataFrame with one row for each element and one column for each regex capture group
extractall() Call re.findall on each element, returning DataFrame with one row for each match and one column for each regex capture group
len() Compute string lengths
strip() Equivalent to str.strip
rstrip() Equivalent to str.rstrip
lstrip() Equivalent to str.lstrip
partition() Equivalent to str.partition
rpartition() Equivalent to str.rpartition
lower() Equivalent to str.lower
casefold() Equivalent to str.casefold
upper() Equivalent to str.upper
find() Equivalent to str.find
rfind() Equivalent to str.rfind
index() Equivalent to str.index
rindex() Equivalent to str.rindex
capitalize() Equivalent to str.capitalize
swapcase() Equivalent to str.swapcase
normalize() Return Unicode normal form. Equivalent to unicodedata.normalize
translate() Equivalent to str.translate
isalnum() Equivalent to str.isalnum
isalpha() Equivalent to str.isalpha
isdigit() Equivalent to str.isdigit
isspace() Equivalent to str.isspace
islower() Equivalent to str.islower
isupper() Equivalent to str.isupper
istitle() Equivalent to str.istitle
isnumeric() Equivalent to str.isnumeric
isdecimal() Equivalent to str.isdecimal

本文已收录于 http://www.flydean.com/06-python-pandas-text/

最通俗的解读,最深刻的干货,最简洁的教程,众多你不知道的小技巧等你来发现!

欢迎关注我的公众号:「程序那些事」,懂技术,更懂你!

Pandas高级教程之:处理text数据的更多相关文章

  1. Pandas高级教程之:处理缺失数据

    目录 简介 NaN的例子 整数类型的缺失值 Datetimes 类型的缺失值 None 和 np.nan 的转换 缺失值的计算 使用fillna填充NaN数据 使用dropna删除包含NA的数据 插值 ...

  2. Pandas高级教程之:GroupBy用法

    Pandas高级教程之:GroupBy用法 目录 简介 分割数据 多index get_group dropna groups属性 index的层级 group的遍历 聚合操作 通用聚合方法 同时使用 ...

  3. Pandas高级教程之:Dataframe的合并

    目录 简介 使用concat 使用append 使用merge 使用join 覆盖数据 简介 Pandas提供了很多合并Series和Dataframe的强大的功能,通过这些功能可以方便的进行数据分析 ...

  4. Pandas高级教程之:category数据类型

    目录 简介 创建category 使用Series创建 使用DF创建 创建控制 转换为原始类型 categories的操作 获取category的属性 重命名categories 使用add_cate ...

  5. Pandas高级教程之:plot画图详解

    目录 简介 基础画图 其他图像 bar stacked bar barh Histograms box Area Scatter Hexagonal bin Pie 在画图中处理NaN数据 其他作图工 ...

  6. Pandas高级教程之:统计方法

    目录 简介 变动百分百 Covariance协方差 Correlation相关系数 rank等级 简介 数据分析中经常会用到很多统计类的方法,本文将会介绍Pandas中使用到的统计方法. 变动百分百 ...

  7. Pandas高级教程之:window操作

    目录 简介 滚动窗口 Center window Weighted window 加权窗口 扩展窗口 指数加权窗口 简介 在数据统计中,经常需要进行一些范围操作,这些范围我们可以称之为一个window ...

  8. Pandas高级教程之:稀疏数据结构

    目录 简介 Spare data的例子 SparseArray SparseDtype Sparse的属性 Sparse的计算 SparseSeries 和 SparseDataFrame 简介 如果 ...

  9. Pandas高级教程之:自定义选项

    目录 简介 常用选项 get/set 选项 经常使用的选项 最大展示行数 超出数据展示 最大列的宽度 显示精度 零转换的门槛 列头的对齐方向 简介 pandas有一个option系统可以控制panda ...

随机推荐

  1. JVM垃圾回收的三种方式

    * 垃圾回收有三种方式 * 一.清除:将需要回收对象的内存空间存放在内存列表中,当需要为新对象分配内存的时候,就会从内存列表中拿取空间分配.不过这种分配方式有两个缺点 * 第一个缺点是内存空间碎片化, ...

  2. Nebula Graph 的 Ansible 实践

    本文首发于 Nebula Graph 公众号 NebulaGraphCommunity,Follow & 看大厂图数据库技术实践 背景 在 Nebula-Graph 的日常测试中,我们会经常在 ...

  3. centos7安装es6.4.0

    一.首先进入到opt文件夹cd opt二.然后下载es安装包wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearc ...

  4. xxl-job源码阅读二(服务端)

    1.源码入口 xxl-job-admin是一个简单的springboot工程,简单翻看源码,可以很快发现XxlJobAdminConfig入口. @Override public void after ...

  5. 单独跑ltp-20200508 ./runltp

    # cat r3.sh#!/bin/bash # cat r3.sh#!/bin/bashi=1for ((; i<=1000; i++))do/opt/ltp/runltp -s fmtmsg ...

  6. Java JOptionPane 对话框

    如果你对 Java 控制台界面下的输入数据和打印输出结果感到有些乏味和厌倦,希望能够像其他计算机软件一样有一个 GUI 界面(图形用户界面). 那么 JOptionPane 对话框也许会让你眼前一亮, ...

  7. 第9章 case条件语句的应用实践

    case语句企业级生产案例 范例9-7:实现通过传参的方式往/etc/openvpn_authfile.conf里添加用户,具体要求如下. 1)命令用法为: USAGE: sh adduser {-a ...

  8. 在.NET 6中使用DateOnly和TimeOnly

    千呼万唤始出来 在.NET 6(preview 4)中引入了两个期待已久的类型,将作为核心库的一部分.DateOnly和TimeOnly允许开发人员表示DateTime的日期或时间部分.这两个类型为值 ...

  9. 为鸿蒙OS说两句公道话(我对鸿蒙OS的一些看法)

    为鸿蒙说两句公道话 今天看了鸿蒙系统的评测,看完后我感觉很欣慰,为什么这么说 ? 不是很多人吐槽鸿蒙是 Android 套壳吗 ?或者叫鸿蒙 UI 吗?说鸿蒙没有自己的核心技术.看了鸿蒙系统的设计,底 ...

  10. Go语言实现Snowflake雪花算法

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com/archives/527 每次放长假的在家里的时候,总想找点简单的例子来看看实现原理,这 ...