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()返回具有 One-Hot 编码值的 DataFrame。8 contains(pattern)如果子字符串包含在元素中,则为每个元素返回布尔值 True,否则返回 False。9 replace(a,b)替换值a与价值b.10 repeat(value)以指定的次数重复每个元素。11 count(pattern)返回每个元素中出现模式的计数。12 startswith(pattern)如果 Series/Index 中的元素以模式开头,则返回 true。13 endswith(pattern)如果 Series/Index 中的元素以模式结尾,则返回 true。14 find(pattern)返回模式第一次出现的第一个位置。15 findall(pattern)返回所有出现的模式的列表。16 swapcase交换外壳下部/上部。17 islower()检查系列/索引中每个字符串中的所有字符是否为小写。返回布尔值18 isupper()检查系列/索引中每个字符串中的所有字符是否为大写。返回布尔值。19 isnumeric()检查系列/索引中每个字符串中的所有字符是否都是数字。返回布尔值。现在让我们创建一个系列,看看上述所有功能是如何工作的。它的输出如下 -lower()
它的输出如下 -upper()
它的输出如下 -len()
它的输出如下 -strip()
它的输出如下 -split(pattern)
它的输出如下 -cat(sep=pattern)
它的输出如下 -get_dummies()
它的输出如下 -contains ()
它的输出如下 -replace(a,b)
它的输出如下 -
repeat(value)
它的输出如下 -count(pattern)
它的输出如下 -startswith(pattern)
它的输出如下 -endswith(pattern)
它的输出如下 -find(pattern)
它的输出如下 -“-1”表示元素中没有可用的这种模式。findall(pattern)
它的输出如下 -Null list([ ]) 表示元素中不存在这样的模式。swapcase()
它的输出如下 -islower()
它的输出如下 -isupper()
它的输出如下 -isnumeric()
它的输出如下 -
)
它的输出如下 -
repeat(value)
它的输出如下 -
count(pattern)
它的输出如下 -
startswith(pattern)
它的输出如下 -
endswith(pattern)
它的输出如下 -
find(pattern)
它的输出如下 -
“-1”表示元素中没有可用的这种模式。
findall(pattern)
它的输出如下 -
Null list([ ]) 表示元素中不存在这样的模式。
swapcase()
它的输出如下 -
islower()
它的输出如下 -
isupper()
它的输出如下 -
isnumeric()
它的输出如下 -