
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')]
#表示或的關(guān)系用"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
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
SQL Server 中 CONVERT 函數(shù)的日期轉(zhuǎn)換:從基礎(chǔ)用法到實戰(zhàn)優(yōu)化 在 SQL Server 的數(shù)據(jù)處理中,日期格式轉(zhuǎn)換是高頻需求 —— 無論 ...
2025-09-18MySQL 大表拆分與關(guān)聯(lián)查詢效率:打破 “拆分必慢” 的認知誤區(qū) 在 MySQL 數(shù)據(jù)庫管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結(jié)構(gòu)數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結(jié)構(gòu)數(shù)據(jù)(如數(shù)據(jù)庫表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動態(tài)隨機一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價值的專業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫)處理 Excel 數(shù)據(jù)時,“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗與 t 檢驗:差異、適用場景與實踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計學(xué)領(lǐng)域,假設(shè)檢驗是驗證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對象的 text 與 content:區(qū)別、場景與實踐指南 在 Python 進行 HTTP 網(wǎng)絡(luò)請求開發(fā)時(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價值的核心操盤手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請求工具對比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長浮點數(shù)據(jù)的科學(xué)計數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長浮點數(shù)據(jù)時的科學(xué)計數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運營問題、提升執(zhí)行效率的核心手段,其價值 ...
2025-09-12用 SQL 驗證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計” 與 “用戶體驗 ...
2025-09-11塔吉特百貨孕婦營銷案例:數(shù)據(jù)驅(qū)動下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當(dāng)下,精準(zhǔn)營銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務(wù)數(shù)據(jù)分析:概念辨析與協(xié)同價值 在數(shù)據(jù)驅(qū)動決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務(wù)數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實踐到業(yè)務(wù)價值挖掘 在數(shù)據(jù)分析場景中,聚類分析作為 “無監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價值導(dǎo)向 統(tǒng)計模型作為數(shù)據(jù)分析的核心工具,并非簡單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10