方法
方法:
DataFrame.duplicated(subset = None,keep =‘first’ )返回boolean数组 一个bool值代表一行
参数:
subset:用来指定特定的列,默认所有列
keep:{‘first’,‘last’,False},默认’first’
first:标记重复,True除了第一次出现。
last:标记重复,True除了最后一次出现。
False:将所有重复项标记为True。
csv内容
源码及结果
import pandas as pd
if __name__=="__main__":
path = "./test.csv"
# path_other = "./test_.csv"
df = pd.read_csv(path, header=0, names=["DEVICE_ID","LNG", "LAT","TEN_GROUP","WEEKDAY","FLOW"])
# df.to_csv(path, mode="a", index=False, header=False)
print("DEVICE_ID列不同值数目:\n%s\n\n" % str(len(df['DEVICE_ID'].unique())))
#keep=False->重复的均置Ture
t=df.duplicated(subset=["DEVICE_ID"],keep=False)
print(t)
#False的个数即代表不重复的键的个数
print(len(t[t==False]))
#选取不重复的键
print("不重复键:")
print(df[~t])
#运行结果
DEVICE_ID列不同值数目:3
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
10 True
11 True
12 True
13 False
dtype: bool
1
不重复键:
DEVICE_ID LNG LAT TEN_GROUP WEEKDAY FLOW
13 3 123.0 43.0 102 1 29