日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python?replace?空格數據處理的實現_python

作者:溫欣' ? 更新時間: 2022-07-30 編程語言

一、使用replace+空格

ordersdetaildf['商品名稱2']=ordersdetaildf['商品名稱'].apply(lambda x:x.replace(" ",""))

上述代碼表示:在Dataframe當中創建新的一列,名字叫做商品名稱2,是對商品名稱列當中的空格進行去除之后的新的數據。

在這里插入圖片描述

對制表符和換行符等等也可以進行同樣的操作:

ordersdetaildf['商品名稱2']=ordersdetaildf['商品名稱2'].apply(lambda x:x.replace("\n","").replace("\\t\\r",""))

二、使用replace+unicode編碼

但是在某些情況下,我發現僅僅使用replace是無法去除空格的:

ordetgb=ordersdetaildf.groupby('訂單編號',as_index=False)["商品名稱"].apply(lambda x:'|'.join(x.values)).reset_index(drop=True) #替換成|很重要

當我想將相同訂單編號的商品名稱進行合并的時候,發現使用join后會出現很多空格,這是使用replace是無法去除空格

在這里插入圖片描述

在這里插入圖片描述

解決方法:

# 經過excel查詢code(a1)  unicode=u00A0  不間斷空格
ordetgb['商品名稱']=ordetgb['商品名稱'].astype(str).apply(lambda x:x.replace(u"\u00A0",""))

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/wxfighting/article/details/125089179

欄目分類
最近更新