京公网安备 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-05-14在Python数据分析中,Pandas作为核心工具库,凭借简洁高效的数据处理能力,成为数据分析从业者的必备技能。其中,基于两列(或多 ...
2026-05-14 很多人把统计学理解为“一堆公式和计算”,却忽略了它的本质——一门让数据“开口说话”的科学。真正的数据分析高手,不是会 ...
2026-05-14在零售行业存量竞争日趋激烈的当下,客户流失已成为侵蚀企业利润的“隐形杀手”——据行业数据显示,零售企业平均客户流失率高达 ...
2026-05-13当流量红利消退、用户需求日趋多元,“凭经验决策、广撒网投放”的传统营销模式早已难以为继。大数据的崛起,为企业营销提供了全 ...
2026-05-13 许多数据分析师精通Excel函数和SQL查询,但当面对一张上万行的销售明细表,要快速回答“哪个地区销量最高”“哪款产品增长最 ...
2026-05-13【专访摘要】本次CDA持证专访邀请到拥有丰富物流供应链数据分析经验的赖尧,他结合自身在京东、华莱士、兰格赛等企业的从业经历 ...
2026-05-12在手游行业存量竞争日趋激烈、流量成本持续高企的当下,“拉新”早已不是行业核心痛点,“留存”尤其是“付费留存”,成为决定手 ...
2026-05-12 很多数据分析师掌握了Excel函数、会写SQL查询,但当被问到“数据从哪里来”“数据加工有哪些步骤”“如何使用分析工具连接数 ...
2026-05-12用户调研是企业洞察客户需求、优化产品服务、制定运营策略的核心前提,而调研数据的可靠性,直接决定了决策的科学性与有效性。在 ...
2026-05-11在市场竞争日趋激烈、流量成本持续攀升的今天,企业的核心竞争力已从“获取流量”转向“挖掘客户价值”。客户作为企业最宝贵的资 ...
2026-05-11 很多数据分析师精通Excel单元格操作,熟练应用多种公式,但当被问到“表结构数据的基本处理单位是什么”“字段和记录的本质 ...
2026-05-11在互联网运营、产品优化、用户增长等领域,次日留存率是衡量产品价值、用户粘性与运营效果的核心指标,更是判断新用户是否认可产 ...
2026-05-09相关性分析是数据分析领域中用于探究两个或多个变量之间关联强度与方向的核心方法,广泛应用于科研探索、商业决策、医疗研究、社 ...
2026-05-09 数据分析师八成以上的时间在和数据表格打交道,但许多人拿到Excel后习惯性地先算、先分析,结果回头发现漏了一列关键数据, ...
2026-05-09在数据驱动运营的时代,指标是连接业务目标与实际行动的核心桥梁,是企业解读业务现状、发现问题、预判趋势的“量化标尺”。一套 ...
2026-05-08在存量竞争日趋激烈的商业时代,“以客户为中心”早已从口号落地为企业运营的核心逻辑。而客户画像作为打通“了解客户”与“服务 ...
2026-05-08 很多数据分析师每天与Excel打交道,但当被问到“什么是表格结构数据”“它和表结构数据有什么区别”“表格结构数据有哪些核 ...
2026-05-08在数据分析、计量研究等场景中,回归分析是探究变量间量化关系的核心方法,无论是简单的一元线性回归,还是复杂的多元线性回归、 ...
2026-05-07在数据分析、计量研究等场景中,回归分析是探究变量间量化关系的核心方法,无论是简单的一元线性回归,还是复杂的多元线性回归、 ...
2026-05-07