1、获取数据源
可以下载下来后,从 csv 读取;或直接从该网址获取,但是网速较差时,直接从网址获取可能会报错
path = r"D:\01_学习相关\01_编程学习\02_Python\08_数据分析\00_datas\Online_Retail.csv" # 本地存放 csv 的绝对路径
online_rt = pd.read_csv(path)
online_rt
# Out[526]:
# InvoiceNo,StockCode,Description,Quantity,InvoiceDate,UnitPrice,CustomerID,Country
# 0 536365,85123A,WHITE HANGING HEART T-LIGHT HOLD...
# 1 536365,71053,WHITE METAL LANTERN,6,12/1/10 8:2...
# 2 536365,84406B,CREAM CUPID HEARTS COAT HANGER,8...
# 3 536365,84029G,KNITTED UNION FLAG HOT WATER BOT...
# 4 536365,84029E,RED WOOLLY HOTTIE WHITE HEART.,6...
# ...
# 50139 540547,84913B,MINT GREEN ROSE TOWEL,4,1/9/11 1...
# 50140 540547,84913A,SOFT PINK ROSE TOWEL ,4,1/9/11 1...
# 50141 540547,C2,CARRIAGE,1,1/9/11 16:07,50,14911,EIRE
# 50142 540548,22726,ALARM CLOCK BAKELIKE GREEN,4,1/9/...
# 50143 540548,22727,ALARM CLOCK BAKELIKE RED ,4,1/9/1...
# [50144 rows x 1 columns]
从下面这张图中,可以发现 Description 列
的字符串中有很多包含单引号,所以通过单引号进行分割时,得到的 DataFrame
有10列
,多出2列
2、使用 正则
对数据进行分割
pd.DataFrame(list(online_rt[online_rt.columns[0]].str.split(',')))
# Out[531]:
# 0 1 ... 8 9
# 0 536365 85123A ... None None
# 1 536365 71053 ... None None
# 2 536365 84406B ... None None
# 3 536365 84029G ... None None
# 4 536365 84029E ... None None
# ... ... ... ... ...
# 50139 540547 84913B ... None None
# 50140 540547 84913A ... None None
# 50141 540547 C2 ... None None
# 50142 540548 22726 ... None None
# 50143 540548 22727 ... None None
# [50144 rows x 10 columns]
思路一: 使用 str.replace(r'(".*),(.*")', r'\1 \2', regex=True)
将 双引号内的逗号 替换为 空格
online_rt.iloc[956, :].values # 原数据
# Out[529]:
# array(['536520,22760,"TRAY, BREAKFAST IN BED",1,12/1/10 12:43,12.75,14729,United Kingdom'],
# dtype=object)
online_rt[online_rt.columns[0]].str.replace(r'(".*),(.*")', r'\1 \2', regex=True)[956] # 替换后的结果
# Out[533]: '536520,22760,"TRAY BREAKFAST IN BED",1,12/1/10 12:43,12.75,14729,United Kingdom'
""" 但是,该规则仅适用于双引号之间只有一个逗号的情况,当逗号个数大于1时,还是会出错。如下: """
online_rt.iloc[2678].to_numpy() # 原数据
# Out[634]:
# array(['536592,22245,"HOOK, 1 HANGER ,MAGIC GARDEN",2,12/1/10 17:06,1.66,,United Kingdom'],
dtype=object)
online_rt[online_rt.columns[0]].str.replace(r'(".*),(.*")', r'\1 \2', regex=True)[2678] # 替换后的结果
# Out[631]: '536592,22245,"HOOK, 1 HANGER MAGIC GARDEN",2,12/1/10 17:06,1.66,,United Kingdom'
思路二: 先定义一个 repl
函数,该函数的作用是,给成功匹配到的数据中的逗号 全部替换为空格,再使用 str.replace(r'"(.*)"', repl, regex=True)
正则,实现整个 DataFrame
的替换
def repl(matchobj):
# print(matchobj.group(1))
return matchobj.group(1).replace(",", " ")
ret = pd.DataFrame(online_rt[online_rt.columns[0]].str.replace(r'"(.*)"', repl, regex=True).str.split(",").to_list())
ret # 结果中的 DataFrame 正好 8 列,成功实现目标
# Out[716]:
# 0 1 ... 6 7
# 0 536365 85123A ... 17850 United Kingdom
# 1 536365 71053 ... 17850 United Kingdom
# 2 536365 84406B ... 17850 United Kingdom
# 3 536365 84029G ... 17850 United Kingdom
# 4 536365 84029E ... 17850 United Kingdom
# ... ... ... ... ...
# 50139 540547 84913B ... 14911 EIRE
# 50140 540547 84913A ... 14911 EIRE
# 50141 540547 C2 ... 14911 EIRE
# 50142 540548 22726 ... 14794 United Kingdom
# 50143 540548 22727 ... 14794 United Kingdom
# [50144 rows x 8 columns]