水军大提督 发表于 2022-8-12 08:08:20

Pandas:文本处理

操作方法 .str.
方法属性 s.str.lower() etc.
切分替换 .str.split('_').str.get(1) .str.replace('^.a|dog', 'XX-XX ', case=False)
连接 .str.cat(sep=',')
索引 s.str
提取子串 .str.extract("(?P)")
1、数据类型

object 和 StringDtype 是 Pandas 的两个文本类型,不过作为新的数据类型,官方推荐 StringDtype 的使用
默认情况下,文本数据会被推断为 object 类型。
string 类型需要专门进行指定:
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625183850345-543593661.png
2、字符的操作

Series 和 Index 都用一些字符串处理方法,可以方便地进行操作,这些方法会自动排除丢失值和 NA 值。我们可以通过 str 属性访问它的方法,进行操作。
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184115304-1195234041.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184120040-1962315603.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184124923-667609049.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184129169-2041606765.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184133587-469047935.png
3、文本高级处理

对文本的分隔和替换是最常用的文本处理方式。对文本分隔后会生成一个列表,我们对列表进行切片操作,可以找到我们想要的内容,分隔后还可以将分隔内容展开,形成单独的行。
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184646806-169779202.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184653325-273307932.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184704837-2054086182.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184709703-682719689.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184714165-1122145142.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184718577-675111508.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184736872-1004593207.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184741297-1536775097.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184812957-2017344912.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184802502-1718449472.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184822291-2101816409.png
https://img2022.cnblogs.com/blog/2145457/202206/2145457-20220625184826588-362014951.png
4、参考文献

《深入浅出Pandas》

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Pandas:文本处理