Pandas | 11 字符串函数

在本章中,咱们将使用基本系列/索引来讨论字符串操做。在随后的章节中,将学习如何将这些字符串函数应用于数据帧(DataFrame)。html

Pandas提供了一组字符串函数,能够方便地对字符串数据进行操做。 最重要的是,这些函数忽略(或排除)丢失/NaN值。python

几乎这些方法都使用Python字符串函数(请参阅: http://docs.python.org/3/library/stdtypes.html#string-methods )。 所以,将Series对象转换为String对象,而后执行该操做。shell

下面来看看每一个操做的执行和说明。函数

编号 函数 描述
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, '1234','SteveMinsu'])
print (s)

输出结果:编码

0 Tom 1 William Rick 2 John 3 Alber@t 4 NaN 5 1234 6 SteveMinsu dtype: object
 

1. lower()函数示例spa

import pandas as pd
import numpy as np

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','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()函数示例code

import pandas as pd
import numpy as np

s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveMinsu'])
print (s.str.upper())

输出结果:htm

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, '1234','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', '1199','William Rick', 'John', 'Alber@t'])
print (s.str.isnumeric())

输出结果:

0 False 1 True 2 False 3 False 4 False dtype: bool
相关文章
相关标签/搜索