当前位置: 首页 > article >正文

pandas字符串操作:大小写转换、连接、分割、包含等

大小写转换

import pandas as pd

data = {
  'text': ['Hello World', 'Python is Great', 'Data Science']
}
df = pd.DataFrame(data)
df.dropna(thresh=True)
c = df["text"].str.capitalize()
# 0        Hello world
# 1    Python is great
# 2       Data science
# Name: text, dtype: object

c = df["text"].str.upper()
# 0        HELLO WORLD
# 1    PYTHON IS GREAT
# 2       DATA SCIENCE

c = df["text"].str.title()
# 0        Hello World
# 1    Python Is Great
# 2       Data Science

c = df["text"].str.lower()
# 0        hello world
# 1    python is great
# 2       data science

c = df["text"].str.swapcase()
# 0        hELLO wORLD
# 1    pYTHON IS gREAT
# 2       dATA sCIENCE

c = df["text"].str.casefold()
# 0        hello world
# 1    python is great
# 2       data science

字符串连接和分割

c = df["text"].str.cat(sep=";")
# Hello World;Python is Great;Data Science

按照分号连接。

sp = df["text"].str.split()
# 0         [Hello, World]
# 1    [Python, is, Great]
# 2        [Data, Science]

分割字符串

包含、以某字符串结尾

c = df["text"].str.contains('is')
# 0    False
# 1     True
# 2    False

支持正则表达式。

c = df["text"].str.endswith("e")
# 0    False
# 1    False
# 2     True
c = df["text"].str.startswith("D")

正则提取

import pandas as pd

data = {
    'text': ['Hello World', 'Python is Great', 'Data Science']
}
df = pd.DataFrame(data)
c = df["text"].str.extract("(\w+) (\w+)")
print(c)
# 0   Hello    World
# 1  Python       is
# 2    Data  Science

参考

https://pandas.pydata.org/docs/reference/api/pandas.Series.str.cat.html


http://www.kler.cn/a/132012.html

相关文章:

  • 基于ChatGPT的文本生成艺术框架—WordArt Designer
  • C++实现有理数类 四则运算和输入输出
  • Nacos 配置中心底层原理(1.X版本)
  • 浅谈霍尔电流传感器在UPS蓄电池浮充电流远程监测方案的应用-安科瑞 蒋静
  • Linux使用Docker完整安装Superset3,同时解决please use superset_config.py to override it报错
  • C#写入Datetime到SQL server
  • 沐神深度学习报错 can only concatenate str (not “int“) to str
  • 企业spark案例 —— 出租车轨迹分析(Python)
  • Java通过Lettuce访问Redis主从,哨兵,集群
  • MATLAB 模型预测控制(MPC)控制入门 —— 设计并仿真 MPC 控制器
  • 应用软件提取出来,打包成.EXE文件在别的电脑上能不能安装?
  • Python爬虫动态IP代理防止被封的方法
  • CSS滚动捕获 scroll-snap-align
  • 【蓝桥杯选拔赛真题23】C++计算24 第十二届蓝桥杯青少年创意编程大赛C++编程选拔赛真题解析
  • Leetcode刷题详解——不同路径
  • bug-跨域访问问题
  • Linux三剑客:awk的高级用法
  • 《Deep learning for fine-grained image analysis: A survey》阅读笔记
  • 设计模式--模板方法外观模式
  • Critical:Azure命令行界面(CLI)可能通过GitHub Actions日志暴露敏感信息