Series.str.endswith(pat, na=nan)
Test if the end of each string element matches a pattern.
>>> s = pd.Series(['bat', 'bear', 'caT', np.nan]) >>> s.str.endswith('t') 0 True 1 False 2 False 3 NaN
>>> s.str.endswith('t', na=False) 0 True 1 False 2 False 3 False
Series.str.split(pat=None, n=-1, expand=False)
Split strings around given separator/delimiter.
>>> s = pd.Series(["this is a regular sentence", "https://docs.python.org/3/tutorial/index.html", np.nan])
可指定某字串做切割,例如用“/"切網址,或用“@"切Email
>>> s.str.split(pat = "/") 0 [this is a regular sentence] 1 [https:, , docs.python.org, 3, tutorial, index... 2 NaN
也可以指定切的字串數量和方向
>>> s.str.split(n=2)
0 [this, is, a regular sentence]
1 [https://docs.python.org/3/tutorial/index.html]
2 NaN
dtype: object
>>> s.str.rsplit(n=2)
0 [this is a, regular, sentence]
1 [https://docs.python.org/3/tutorial/index.html]
2 NaN
Counter()
>>> from collections import Counter >>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red'] >>> Counter(z) Counter({'blue': 3, 'red': 2, 'yellow': 1})
words = ['a', 'b', 'c', 'a'] Counter(words).keys() # equals to list(set(words)) Counter(words).values()# counts the elements' frequency
可分別列出 list(set(words)) 和 elements’ frequency
Output:
['a', 'c', 'b'] [2, 1, 1]
也可以frequency最高的:
>>> from collections import Counter >>> A = {'a' : 1, 'b' : 3, 'c' : 2, 'd' : 4, 'e' : 0, 'f' :5} >>> dict(Counter(A).most_common(5)) {'a': 1, 'c': 2, 'b': 3, 'd': 4, 'f': 5}