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 |
分裂(' ') 用给定的模式分割每个字符串。 |
6 |
猫(九月='') 用给定的分隔符连接系列/索引元素。 |
7 |
get_dummies() 返回具有 One-Hot 编码值的 DataFrame。 |
8 |
包含(模式) 如果子字符串包含在元素中,则为每个元素返回布尔值 True,否则返回 False。 |
9 |
替换(a,b) 替换值 a 与价值 b . |
10 |
重复(值) 以指定的次数重复每个元素。 |
11 |
计数(模式) 返回每个元素中出现模式的计数。 |
12 |
开始于(模式) 如果 Series/Index 中的元素以模式开头,则返回 true。 |
13 |
以(模式)结尾 如果 Series/Index 中的元素以模式结尾,则返回 true。 |
14 |
查找(模式) 返回模式第一次出现的第一个位置。 |
15 |
findall(模式) 返回所有出现的模式的列表。 |
16 |
swapcase 交换外壳下部/上部。 |
17 |
下层() 检查系列/索引中每个字符串中的所有字符是否为小写。返回布尔值 |
18 |
isupper() 检查系列/索引中每个字符串中的所有字符是否为大写。返回布尔值。 |
19 |
非数字() 检查每个字符串中的所有字符是否 系列/索引是数字。返回布尔值。 |
现在让我们创建一个系列,看看上述所有功能是如何工作的。
import pandas as pd import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveSmith']) print s
Its output 如下:
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()
Its output 如下:
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()
Its output 如下:
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()
Its output 如下:
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()
Its output 如下:
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
分裂(模式)
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(' ')
Its output 如下:
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
猫(sep=模式)
import pandas as pd import numpy as np s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s.str.cat(sep='_')
Its output 如下:
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()
Its output 如下:
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
包含 ()
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s.str.contains(' ')
Its output 如下:
0 True 1 True 2 False 3 False dtype: bool
替换(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('@','$')
Its output 如下:
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
重复(值)
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s.str.repeat(2)
Its output 如下:
0 Tom Tom 1 William Rick William Rick 2 JohnJohn 3 Alber@tAlber@t dtype: object
计数(模式)
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')
Its output 如下:
The number of 'm's in each string: 0 1 1 1 2 0 3 0
开始于(模式)
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print ("Strings that start with 'T':") print s.str. startswith ('T')
Its output 如下:
0 True 1 False 2 False 3 False dtype: bool
以(模式)结尾
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print ("Strings that end with 't':") print s.str.endswith('t')
Its output 如下:
Strings that end with 't': 0 False 1 False 2 False 3 True dtype: bool
查找(模式)
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s.str.find('e')
Its output 如下:
0 -1 1 -1 2 -1 3 3 dtype: int64
“-1”表示元素中没有可用的这种模式。
findall(模式)
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s.str.findall('e')
Its output 如下:
0 [] 1 [] 2 [] 3 [e] dtype: object
Null list([ ]) 表示元素中不存在这样的模式。
交换箱()
import pandas as pd s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) print s.str.swapcase()
Its output 如下:
0 tOM 1 wILLIAM rICK 2 jOHN 3 aLBER@T dtype: object
下层()
import pandas as pd s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) print s.str.islower()
Its output 如下:
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()
Its output 如下:
0 False 1 False 2 False 3 False dtype: bool
非数字()
import pandas as pd s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) print s.str.isnumeric()
Its output 如下:
0 False 1 False 2 False 3 False dtype: bool