資料去重可以使用duplicated()和drop_duplicates()兩個方法。
dataframe.duplicated(subset = none,keep =『first』)返回boolean series表示重複行
引數:subset:列標籤或標籤序列,可選
僅考慮用於標識重複項的某些列,預設情況下使用所有列
keep:,預設』first』
first:標記重複,true除了第一次出現。
last:標記重複,true除了最後一次出現。
錯誤:將所有重複項標記為true。
import pandas as pd
from pandas import series, dataframe
df = pd.read_csv('./demo_duplicate.csv')
print(df)
print(df['seqno'].unique()) # [0. 1.]
# 使用duplicated 檢視重複值
# 引數 keep 可以標記重複值
print(df['seqno'].duplicated())
0 false
1 true
2 true
3 true
4 false
name: seqno, dtype: bool
# 刪除 series 重複資料
print(df['seqno'].drop_duplicates())
0 0.0
4 1.0
name: seqno, dtype: float64
# 刪除 dataframe 重複資料
print(df.drop_duplicates(['seqno'])) # 按照 seqno 來去重
price seqno symbol time
# drop_dujplicates() 第二個引數 keep 包含的值 有: first、last、false
print(df.drop_duplicates(['seqno'], keep='last')) # 儲存最後乙個
price seqno symbol time
python中列表去重
li 2,5,4,7,2,1,3,2,6,7 print 方法一列表追加 center 40,list1 for i in li if i not in list1 print list1 print 推導式實現 center 40,list2 print list2 print 方法二利用字典鍵唯...
python中列表去重
列表去重 定義乙個函式 def remove element m list 將列表 10,1,2,20,10,3,2,1,15,20,44,56,3,2,1 去除重複元素 def remove element m list 定義乙個空列表 new list 迴圈判斷某個元素是不是在新的列表中,如果在...
python 多表去重 Python列表去重
無聊統計了下列表去重到底有多少種方法。1.集合 list set alist 如果要保持順序 import random if name main a random.randint 0,10 for i in xrange 10 b list set a b.sort key a.index 2.字...