Python Pandas 处理文本数据
在本章中,我们将使用我们的基本系列/索引来讨论字符串操作。在后续章节中,我们将学习如何在 DataFrame 上应用这些字符串函数。
Pandas 提供了一组字符串函数,可以很容易地对字符串数据进行操作。最重要的是,这些函数忽略(或排除)缺失/NaN 值。
几乎,所有这些方法都适用于 Python 字符串函数(请参阅: https://docs.python.org/3/library/stdtypes.html#string-methods )。因此,将系列对象转换为字符串对象,然后执行操作。
现在让我们看看每个操作是如何执行的。
序号 | 函数说明 |
---|---|
1 | lower() 将系列/索引中的字符串转换为小写。 |
2 | upper() 将系列/索引中的字符串转换为大写。 |
3 | len() 计算字符串长度()。 |
4 | strip() 帮助从两侧去除系列/索引中每个字符串的空格(包括换行符)。 |
5 | split(' ') 使用给定的模式拆分每个字符串。 |
6 | cat(sep='') 使用给定的分隔符连接系列/索引元素。 |
7 | get_dummies() 返回具有 One-Hot 编码值的 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, '1234','SteveSmith']) print s
输出如下:
0 Tom 1 William Rick 2 John 3 Alber@t 4 NaN 5 1234 6 Steve Smith dtype: object
lower()
import pandas as pd import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith']) print s.str.lower()
输出如下:
0 tom 1 william rick 2 john 3 alber@t 4 NaN 5 1234 6 steve smith dtype: object
upper()
import pandas as pd import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith']) print s.str.upper()
输出如下:
0 TOM 1 WILLIAM RICK 2 JOHN 3 ALBER@T 4 NaN 5 1234 6 STEVE SMITH dtype: object
len()
import pandas as pd import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith']) 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
strip()
import pandas as pd import numpy as np s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s 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
split(pattern)
import pandas as pd import numpy as np s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s 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
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
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
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
replace(a,b)
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s 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
repeat(值)
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
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
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')
输出如下:
0 True 1 False 2 False 3 False dtype: bool
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
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”表示元素中没有这样的模式可用。
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
空list([ ]) 表示元素中没有这样的模式可用。
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
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
isupper()
import pandas as pd s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) print s.str.isupper()
输出如下:
0 False 1 False 2 False 3 False dtype: bool
isnumeric()
import pandas as pd s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) print s.str.isnumeric()
输出如下:
0 False 1 False 2 False 3 False dtype: bool