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() 返回带有一热编码值的数据框架。 |
8 | contains(pattern) 如果子串包含在元素中,则为每个元素返回一个布尔值True,否则为False。 |
9 | replace(a,b) 用 b 的值替换 a 的值 。 |
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 Albert
dtype: object
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
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(mode)
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
Null 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