京公网安备 11010802034615号
经营许可证编号:京B2-20210330
import pandas as pd
d = pd.DataFrame(['a', 'b', 'c'],columns = ['A'])
d
| A | |
|---|---|
| 0 | a |
| 1 | b |
| 2 | c |
将某列元素拼接一列特定字符串
d['A'].str.cat(['A', 'B', 'C'], sep=',')
0 a,A
1 b,B
2 c,C
Name: A, dtype: object
将某列的元素合并为一个字符串
d['A'].str.cat(sep=',')
'a,b,c'
import numpy as np
import pandas as pd
d = pd.DataFrame(['a_b_c', 'c_d_e', np.nan, 'f_g_h'],columns = ['A'])
d
| A | |
|---|---|
| 0 | a_b_c |
| 1 | c_d_e |
| 2 | NaN |
| 3 | f_g_h |
将某列的字符串元素进行切分
d['A'].str.split('_')
0 [a, b, c]
1 [c, d, e]
2 NaN
3 [f, g, h]
Name: A, dtype: object
d = pd.DataFrame(['a_b_c', 'c_d_e', np.nan, 'f_g_h'],columns = ['A'])
d['A']
0 a_b_c
1 c_d_e
2 NaN
3 f_g_h
Name: A, dtype: object
d['A'].str.get(2)
0 b
1 d
2 NaN
3 g
Name: A, dtype: object
d = pd.DataFrame(['a_b_c', 'c_d_e', np.nan, 'f_g_h'],columns = ['A'])
d['A']
0 a_b_c
1 c_d_e
2 NaN
3 f_g_h
Name: A, dtype: object
d['A'].str.join("!")
0 a!_!b!_!c
1 c!_!d!_!e
2 NaN
3 f!_!g!_!h
Name: A, dtype: object
d['A'].str.contains('d')
0 False
1 True
2 NaN
3 False
Name: A, dtype: object
d.fillna('0')[d.fillna('0')['A'].str.contains('d')]
| A | |
|---|---|
| 1 | c_d_e |
d.fillna('0')[d['A'].fillna('0').str.contains('d|e')]
#表示或的关系用"A|B",表示且用'A.*B|B.*A'
| A | |
|---|---|
| 1 | c_d_e |
d['A'].str.replace("_", ".")
0 a.b.c
1 c.d.e
2 NaN
3 f.g.h
Name: A, dtype: object
d['A'].str.repeat(3)
0 a_b_ca_b_ca_b_c
1 c_d_ec_d_ec_d_e
2 NaN
3 f_g_hf_g_hf_g_h
Name: A, dtype: object
d['A'].str.pad(10, fillchar="0")
0 00000a_b_c
1 00000c_d_e
2 NaN
3 00000f_g_h
Name: A, dtype: object
d['A'].str.pad(10, side="right", fillchar="?")
0 a_b_c?????
1 c_d_e?????
2 NaN
3 f_g_h?????
Name: A, dtype: object
d['A'].str.center(10, fillchar="?")
0 ??a_b_c???
1 ??c_d_e???
2 NaN
3 ??f_g_h???
Name: A, dtype: object
d['A'].str.ljust(10, fillchar="?")
0 a_b_c?????
1 c_d_e?????
2 NaN
3 f_g_h?????
Name: A, dtype: object
d['A'].str.rjust(10, fillchar="?")
0 ?????a_b_c
1 ?????c_d_e
2 NaN
3 ?????f_g_h
Name: A, dtype: object
d['A'].str.zfill(10)
0 00000a_b_c
1 00000c_d_e
2 NaN
3 00000f_g_h
Name: A, dtype: object
d['A'].str.wrap(3)
0 a_bn_c
1 c_dn_e
2 NaN
3 f_gn_h
Name: A, dtype: object
d['A'].str.slice(1,3)
0 _b
1 _d
2 NaN
3 _g
Name: A, dtype: object
d['A'].str.slice_replace(1, 3, "?")
0 a?_c
1 c?_e
2 NaN
3 f?_h
Name: A, dtype: object
d['A'].str.count("b")
0 1.0
1 0.0
2 NaN
3 0.0
Name: A, dtype: float64
d['A'].str.startswith("a")
0 True
1 False
2 NaN
3 False
Name: A, dtype: object
d['A'].str.endswith("e")
0 False
1 True
2 NaN
3 False
Name: A, dtype: object
d['A'].str.findall("[a-z]")
0 [a, b, c]
1 [c, d, e]
2 NaN
3 [f, g, h]
Name: A, dtype: object
d['A'].str.match("[d-z]")
0 False
1 False
2 NaN
3 True
Name: A, dtype: object
d['A'].str.extract("([d-z])")
| 0 | |
|---|---|
| 0 | NaN |
| 1 | d |
| 2 | NaN |
| 3 | f |
d['A'].str.len()
0 5.0
1 5.0
2 NaN
3 5.0
Name: A, dtype: float64
df = pd.DataFrame(['a_b ', ' d_e ', np.nan, 'f_g '],columns = ['B'])
df['B']
0 a_b
1 d_e
2 NaN
3 f_g
Name: B, dtype: object
df['B'].str.strip()
0 a_b
1 d_e
2 NaN
3 f_g
Name: B, dtype: object
df['B'].str.rstrip()
0 a_b
1 d_e
2 NaN
3 f_g
Name: B, dtype: object
df['B'].str.lstrip()
0 a_b
1 d_e
2 NaN
3 f_g
Name: B, dtype: object
d['A'] .str.partition('_')
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | a | _ | b_c |
| 1 | c | _ | d_e |
| 2 | NaN | NaN | NaN |
| 3 | f | _ | g_h |
d['A'].str.rpartition('_')
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | a_b | _ | c |
| 1 | c_d | _ | e |
| 2 | NaN | NaN | NaN |
| 3 | f_g | _ | h |
d['A'].str.lower()
0 a_b_c
1 c_d_e
2 NaN
3 f_g_h
Name: A, dtype: object
d['A'].str.upper()
0 A_B_C
1 C_D_E
2 NaN
3 F_G_H
Name: A, dtype: object
d['A'].str.find('d')
0 -1.0
1 2.0
2 NaN
3 -1.0
Name: A, dtype: float64
d['A'].str.rfind('d')
0 -1.0
1 2.0
2 NaN
3 -1.0
Name: A, dtype: float64
d['A'].str.index('_')
0 1.0
1 1.0
2 NaN
3 1.0
Name: A, dtype: float64
d['A'].str.rindex('_')
0 3.0
1 3.0
2 NaN
3 3.0
Name: A, dtype: float64
d['A'].str.capitalize()
0 A_b_c
1 C_d_e
2 NaN
3 F_g_h
Name: A, dtype: object
d['A'].str.capitalize()
0 A_b_c
1 C_d_e
2 NaN
3 F_g_h
Name: A, dtype: object
d['A'].str.isalnum()
0 False
1 False
2 NaN
3 False
Name: A, dtype: object
d['A'].str.isalpha()
0 False
1 False
2 NaN
3 False
Name: A, dtype: object
d['A'].str.isdigit()
0 False
1 False
2 NaN
3 False
Name: A, dtype: object
d['A'].str.isspace()
0 False
1 False
2 NaN
3 False
Name: A, dtype: object
d['A'].str.islower()
0 True
1 True
2 NaN
3 True
Name: A, dtype: object
d['A'].str.isupper()
0 False
1 False
2 NaN
3 False
Name: A, dtype: object
d['A'].str.istitle()
0 False
1 False
2 NaN
3 False
Name: A, dtype: object
d['A'].str.isnumeric()
0 False
1 False
2 NaN
3 False
Name: A, dtype: object
d['A'].str.isdecimal()
0 False
1 False
2 NaN
3 False
Name: A, dtype: object
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
在数据分析、市场研究、用户画像构建、学术研究等场景中,我们常常会遇到多维度、多指标的数据难题:比如调研用户消费行为时,收 ...
2026-03-25在流量红利见顶、获客成本持续攀升的当下,营销正从“广撒网”的经验主义,转向“精耕细作”的数据驱动主义。数据不再是营销的辅 ...
2026-03-25在CDA(Certified Data Analyst)数据分析师的全流程工作中,无论是前期的数据探索、影响因素排查,还是中期的特征筛选、模型搭 ...
2026-03-25在当下数据驱动决策的职场环境中,A/B测试早已成为互联网产品、运营、营销乃至产品迭代优化的核心手段,小到一个按钮的颜色、文 ...
2026-03-24在统计学数据分析中,尤其是分类数据的分析场景里,卡方检验和显著性检验是两个高频出现的概念,很多初学者甚至有一定统计基础的 ...
2026-03-24在CDA(Certified Data Analyst)数据分析师的日常业务分析与统计建模工作中,多组数据差异对比是高频且核心的分析场景。比如验 ...
2026-03-24日常用Excel做数据管理、台账维护、报表整理时,添加备注列是高频操作——用来标注异常、说明业务背景、记录处理进度、补充关键 ...
2026-03-23作为业内主流的自助式数据可视化工具,Tableau凭借拖拽式操作、强大的数据联动能力、灵活的仪表板搭建,成为数据分析师、业务人 ...
2026-03-23在CDA(Certified Data Analyst)数据分析师的日常工作与认证考核中,分类变量的关联分析是高频核心场景。用户性别是否影响商品 ...
2026-03-23在数据工作的全流程中,数据清洗是最基础、最耗时,同时也是最关键的核心环节,无论后续是做常规数据分析、可视化报表,还是开展 ...
2026-03-20在大数据与数据驱动决策的当下,“数据分析”与“数据挖掘”是高频出现的两个核心概念,也是很多职场人、入门学习者容易混淆的术 ...
2026-03-20在CDA(Certified Data Analyst)数据分析师的全流程工作闭环中,统计制图是连接严谨统计分析与高效业务沟通的关键纽带,更是CDA ...
2026-03-20在MySQL数据库优化中,分区表是处理海量数据的核心手段——通过将大表按分区键(如时间、地域、ID范围)分割为多个独立的小分区 ...
2026-03-19在商业智能与数据可视化领域,同比、环比增长率是分析数据变化趋势的核心指标——同比(YoY)聚焦“长期趋势”,通过当前周期与 ...
2026-03-19在数据分析与建模领域,流传着一句行业共识:“数据决定上限,特征决定下限”。对CDA(Certified Data Analyst)数据分析师而言 ...
2026-03-19机器学习算法工程的核心价值,在于将理论算法转化为可落地、可复用、高可靠的工程化解决方案,解决实际业务中的痛点问题。不同于 ...
2026-03-18在动态系统状态估计与目标跟踪领域,高精度、高鲁棒性的状态感知是机器人导航、自动驾驶、工业控制、目标检测等场景的核心需求。 ...
2026-03-18“垃圾数据进,垃圾结果出”,这是数据分析领域的黄金法则,更是CDA(Certified Data Analyst)数据分析师日常工作中时刻恪守的 ...
2026-03-18在机器学习建模中,决策树模型因其结构直观、易于理解、无需复杂数据预处理等优势,成为分类与回归任务的首选工具之一。而变量重 ...
2026-03-17在数据分析中,卡方检验是一类基于卡方分布的假设检验方法,核心用于分析分类变量之间的关联关系或实际观测分布与理论期望分布的 ...
2026-03-17