網站首頁 編程語言 正文
前言
當我們使用pandas處理數據的時候,經常會遇到數據重復的問題,如何找出重復數據進而分析重復原因,或者如何直接刪除重復的數據是一個關鍵的步驟,pandas提供了很方便的方法:duplicated()和drop_duplicates()。
一、duplicated()
duplicated()可以被用在DataFrame的三種情況下,分別是pandas.DataFrame.duplicated、pandas.Series.duplicated和pandas.Index.duplicated。他們的用法都類似,前兩個會返回一個布爾值的Series,最后一個會返回一個布爾值的numpy.ndarray。
DataFrame.duplicated(subset=None, keep=‘first’)
subset:默認為None,需要標記重復的標簽或標簽序列
keep:默認為‘first’,如何標記重復標簽
- first:將除第一次出現以外的重復數據標記為True
- last:將除最后一次出現以外的重復數據標記為True
- False:將所有重復的項都標記為True(不管是不是第一次出現)
Series.duplicated(keep=‘first’)
keep:與DataFrame.duplicated的keep相同
Index.duplicated(keep=‘first’)
keep:與DataFrame.duplicated的keep相同
例子:
import pandas as pd
df = pd.DataFrame({
'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
'rating': [4, 4, 3.5, 15, 5]
})
df
? ? brand style ?rating
0 ?Yum Yum ? cup ? ? 4.0
1 ?Yum Yum ? cup ? ? 4.0
2 ?Indomie ? cup ? ? 3.5
3 ?Indomie ?pack ? ?15.0
4 ?Indomie ?pack ? ? 5.0?
df.duplicated()
0 ? ?False
1 ? ? True
2 ? ?False
3 ? ?False
4 ? ?False
dtype: bool
df.duplicated(keep='last')
0 ? ? True
1 ? ?False
2 ? ?False
3 ? ?False
4 ? ?False
dtype: bool
df.duplicated(keep=False)
0 ? ? True
1 ? ? True
2 ? ?False
3 ? ?False
4 ? ?False
dtype: bool
df.duplicated(subset=['brand'])
0 ? ?False
1 ? ? True
2 ? ?False
3 ? ? True
4 ? ? True
dtype: bool
關于Index的重復標記:
df = df.set_index('brand')
df
? ? ? ? style ?rating
brand ? ? ? ? ? ? ? ?
Yum Yum ? cup ? ? 4.0
Yum Yum ? cup ? ? 4.0
Indomie ? cup ? ? 3.5
Indomie ?pack ? ?15.0
Indomie ?pack ? ? 5.0
df.index.duplicated()
array([False, True, False, True, True])
二、drop_duplicates()
與duplicated()類似,drop_duplicates()是直接把重復值給刪掉。下面只會介紹一些含義不同的參數。
DataFrame.drop_duplicates(subset=None, keep=‘first’, inplace=False)
- subset:與duplicated()中相同
- keep:與duplicated()中相同
- inplace:與pandas其他函數的inplace相同,選擇是修改現有數據還是返回新的數據
Series.drop_duplicates()相比Series.duplicated()也是多了一個inplace參數,和上訴介紹一樣,Index.drop_duplicates()與Index.duplicated()參數相同就不做贅述。下面是例子:
df = pd.DataFrame({
'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
'rating': [4, 4, 3.5, 15, 5]
})
df
? ? ?brand style ?rating
0 ?Yum Yum ? cup ? ? 4.0
1 ?Yum Yum ? cup ? ? 4.0
2 ?Indomie ? cup ? ? 3.5
3 ?Indomie ?pack ? ?15.0
4 ?Indomie ?pack ? ? 5.0
df.drop_duplicates()
? ? ?brand style ?rating
0 ?Yum Yum ? cup ? ? 4.0
2 ?Indomie ? cup ? ? 3.5
3 ?Indomie ?pack ? ?15.0
4 ?Indomie ?pack ? ? 5.0
df.drop_duplicates(inplace = True)
df
? ? ?brand style ?rating
0 ?Yum Yum ? cup ? ? 4.0
2 ?Indomie ? cup ? ? 3.5
3 ?Indomie ?pack ? ?15.0
4 ?Indomie ?pack ? ? 5.0
總結
有剩余無,pandas有很多好用的庫,但是系統學下來很不現實,都是在實際項目中不斷的發現、積累、記錄下來。
原文鏈接:https://blog.csdn.net/weixin_43887421/article/details/114926685
相關推薦
- 2022-05-16 C#?CM框架實現多頁面管理的實例代碼_C#教程
- 2022-03-17 Qt編寫提示進度條的實現示例_C 語言
- 2022-05-23 React中setState同步異步場景的使用_React
- 2022-07-15 Python標準庫之Math,Random模塊使用詳解_python
- 2022-06-04 Android自定義scrollview實現回彈效果_Android
- 2022-12-19 C++?Boost?Coroutine使用協程詳解_C 語言
- 2023-03-01 shell輸出重定向的實現_linux shell
- 2022-08-03 python數據類型可變與不可變深入分析_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支