English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Pandas-Textverarbeitungsoperationen-Beispiel
In diesem Kapitel werden wir grundlegende Series / Index diskutiert Stringoperationen. In den folgenden Kapiteln werden wir lernen, wie man diese Stringfunktionen auf DataFrames anwendet.
Pandas bietet eine Reihe von Stringfunktionen, mit denen Stringdaten leicht bearbeitet werden können. Wichtig ist, dass diese Funktionen fehlende/ NaN-Werte.
Fast alle diese Methoden können für Python-Stringfunktionen verwendet werden (siehe: https://docs.python.org/3/library/stdtypes.html#string-methods)。因此,将Series对象转换为String对象,然后执行该操作。
我们看看每个操作如何执行。
方法 | 说明 |
lower() | 将系列/索引中的字符串转换为小写。 |
upper() | 将系列/索引中的字符串转换为大写。 |
len() | 计算字符串length()。 |
strip() | 帮助从两侧从系列/索引中的每个字符串中去除空格(包括换行符)。 |
split(' ') | 用给定的模式分割每个字符串。 |
cat(sep=' ')/td> | 用给定的分隔符连接系列/索引元素。 |
get_dummies() | 返回具有一键编码值的DataFrame。 |
contains(pattern) | 如果子字符串包含在元素中,则为每个元素返回一个布尔值True,否则返回False。 |
replace(a,b) | a值替换成b。 |
repeat(value) | 以指定的次数重复每个元素。 |
count(pattern) | 返回每个元素中模式出现的次数。 |
startswith(pattern) | 如果系列/索引中的元素以模式开头,则返回true。 |
endswith(pattern) | 如果系列/索引中的元素以模式结尾,则返回true。 |
find(pattern) | 返回模式首次出现的第一个位置。 |
findall(pattern) | 返回所有出现的模式的列表。 |
swapcase | 大小写互换 |
islower()< | 检查“系列/索引”中每个字符串中的所有字符是否都小写。返回布尔值 |
isupper() | 检查“系列/索引”中每个字符串中的所有字符是否都大写。返回布尔值。 |
isnumeric() | 检查“系列/索引”中每个字符串中的所有字符是否都是数字。返回布尔值。 |
我们来创建一个Series,看看以上所有功能如何工作。
import pandas as pd import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Albert', np.nan, '1234','SteveSmith']) print s
Laufender Output:
0 Tom 1 William Rick 2 John 3 Alber@t 4 NaN 5 1234 6 Steve Smith dtype: object
import pandas as pd import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Albert', np.nan, '1234','SteveSmith']) print s.str.lower()
Laufender Output:
0 tom 1 william rick 2 john 3 alber@t 4 NaN 5 1234 6 steve smith dtype: object
import pandas as pd import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Albert', np.nan, '1234','SteveSmith']) print s.str.upper()
Laufender Output:
0 TOM 1 WILLIAM RICK 2 JOHN 3 ALBER@T 4 NaN 5 1234 6 STEVE SMITH dtype: object
import pandas as pd import numpy as np s = pd.Series(['Tom', 'William Rick', 'John', 'Albert', np.nan, '1234','SteveSmith']) print(s.str.len())
Laufender Output:
0 3.0 1 12.0 2 4.0 3 7.0 4 NaN 5 4.0 6 10.0 Datentyp: float64
import pandas as pd import numpy as np s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s print(('Nach dem Entfernen:')) print(s.str.strip())
Laufender Output:
0 Tom 1 William Rick 2 John 3 Alber@t dtype: object Nach dem Entfernen: 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(' ')
Laufender 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
import pandas as pd import numpy as np s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s.str.cat(sep='_')
Laufender Output:
Tom _ William Rick_John_Alber@t
import pandas as pd import numpy as np s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s.str.get_dummies()
Laufender Output:
William Rick Alber@t John Tom 0 0 0 0 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(' ')
Laufender Output:
0 True 1 True 2 False 3 False dtype: bool
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s print ("After replacing @ with $:") print s.str.replace('@',') )
Laufender 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)
Laufender 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' in each string:") print s.str.count('m')
Laufender Output:
The number of 'm' 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.startwith('T')
Laufender 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')
Laufender Output:
Zeichenfolgen, die mit 't' enden: 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')
Laufender Output:
0 -1 1 -1 2 -1 3 3 dtype: int64
“ -1” bedeutet, dass keine Übereinstimmungen im Element gefunden wurden.
import pandas as pd s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) print s.str.findall('e')
Laufender Output:
0 [] 1 [] 2 [] 3 [e] dtype: object
Eine leere Liste ([]), die angibt, dass keine Übereinstimmungen im Element gefunden wurden
import pandas as pd s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t']) print s.str.swapcase()
Laufender 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()
Laufender 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.isupper()
Laufender 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()
Laufender Output:
0 False 1 False 2 False 3 False dtype: bool