-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_for_duplicates.py
More file actions
60 lines (48 loc) · 1.73 KB
/
check_for_duplicates.py
File metadata and controls
60 lines (48 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Script to detect duplicates in pandas dataframes
"""
import pandas as pd
from os import listdir, mkdir
from functions import pandas_dataframe_info
from tqdm import tqdm
DIR = "./RESULTS/ED4RE_2503/"
PER = 0.05
dfs = listdir(DIR)
try:
mkdir(DIR+"DEDUPLICATED/")
except:
print("Folder \"DEDUPLICATED\" already exists ...")
for df_name in tqdm(dfs):
print(5*"----")
print("Dataframe name: ", df_name)
try:
df = pd.read_pickle(DIR+df_name)
except:
print("Dataframe doesn't exist or is not a pickle file ...")
continue
# Content length
df["Title"] = df["Title"].apply(lambda x: ' '.join(x))
nrows = len(df)
duplicate_rows = df[df.duplicated(subset=["Title", "Content"])]
# print("Columns: ", ", ".join(df.keys().tolist()))
try:
df = df.drop_duplicates(subset=["Title", "Content"])
except:
print("Exception occured during deduplication ...")
continue
nrows_d = len(df)
# Duplicates
nrows_d_clean = len(duplicate_rows[duplicate_rows["Content"].str.len() > 10]["Title"])
print("Number of rows: ", nrows)
print("Number of duplicates: ", nrows-nrows_d)
print("Real number of duplicates: ", nrows_d_clean)
print(duplicate_rows[duplicate_rows["Content"].str.len() > 10]["Title"])
if nrows_d_clean/nrows > PER:
duplicate_rows[duplicate_rows["Content"].str.len() > 10][["Title", "Content"]].to_csv(DIR+df_name.split(".")[0]+"_duplicate.csv")
print("Dataframe containes: ", (nrows_d_clean/nrows)*100, "\% duplicates")
print("General info: ", "\n", 5*"----")
pandas_dataframe_info(df)
print(5*"----")
print(5*"----")
print()
df.to_pickle(DIR+"DEDUPLICATED/"+df_name)