-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleaning.py
More file actions
37 lines (28 loc) · 915 Bytes
/
cleaning.py
File metadata and controls
37 lines (28 loc) · 915 Bytes
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
import pandas as pd
import numpy as np
def clean_dataset():
df = pd.read_csv('CICIDS dataset.csv')
print(df.shape)
print(df.head())
df.columns = df.columns.str.strip()
print(df["Label"].unique())
#replace infinite values with NaN and drop rows with NaN values
df = df.replace([np.inf, -np.inf], np.nan)
df = df.dropna()
for col in df.columns:
if col == "Label":
continue
df[col] = pd.to_numeric(df[col], errors="coerce")
df = df.dropna()
cols = ["Flow Bytes/s", "Flow Packets/s"]
for c in cols:
if c in df.columns:
median_val = df[c].median()
df[c] = df[c].fillna(median_val)
df = df[df["Flow Duration"].notna() & (df["Flow Duration"] >= 0)]
print(df.shape)
print(df.head())
df.to_csv('CICIDS_cleaned.csv', index=False)
return df
if __name__ == "__main__":
clean_dataset()