how to use feature engineering on a label
I found a dataset about DDOS attacks id like to use but the tool used to extract features from pcap files seems to have indented the columns a little weird as seen below there are indents in unusual places. like at the start of some feature names in particular im interested in the label or in this case Label (where there is a space in front). Syn is the name of the dataframe.
Syn.columns
Index(['Unnamed: 0', 'Flow ID', ' Source IP', ' Source Port', ' Destination IP', ' Destination Port', ' Protocol', ' Timestamp', ' Flow Duration', ' Total Fwd Packets', ' Total Backward Packets', 'Total Length of Fwd Packets', ' Total Length of Bwd Packets', ' Fwd Packet Length Max', ' Fwd Packet Length Min', ' Fwd Packet Length Mean', ' Fwd Packet Length Std', 'Bwd Packet Length Max', ' Bwd Packet Length Min', ' Bwd Packet Length Mean', ' Bwd Packet Length Std', 'Flow Bytes/s', ' Flow Packets/s', ' Flow IAT Mean', ' Flow IAT Std', ' Flow IAT Max', ' Flow IAT Min', 'Fwd IAT Total', ' Fwd IAT Mean', ' Fwd IAT Std', ' Fwd IAT Max', ' Fwd IAT Min', 'Bwd IAT Total', ' Bwd IAT Mean', ' Bwd IAT Std', ' Bwd IAT Max', ' Bwd IAT Min', 'Fwd PSH Flags', ' Bwd PSH Flags', ' Fwd URG Flags', ' Bwd URG Flags', ' Fwd Header Length', ' Bwd Header Length', 'Fwd Packets/s', ' Bwd Packets/s', ' Min Packet Length', ' Max Packet Length', ' Packet Length Mean', ' Packet Length Std', ' Packet Length Variance', 'FIN Flag Count', ' SYN Flag Count', ' RST Flag Count', ' PSH Flag Count', ' ACK Flag Count', ' URG Flag Count', ' CWE Flag Count', ' ECE Flag Count', ' Down/Up Ratio', ' Average Packet Size', ' Avg Fwd Segment Size', ' Avg Bwd Segment Size', ' Fwd Header Length.1', 'Fwd Avg Bytes/Bulk', ' Fwd Avg Packets/Bulk', ' Fwd Avg Bulk Rate', ' Bwd Avg Bytes/Bulk', ' Bwd Avg Packets/Bulk', 'Bwd Avg Bulk Rate', 'Subflow Fwd Packets', ' Subflow Fwd Bytes', ' Subflow Bwd Packets', ' Subflow Bwd Bytes', 'Init_Win_bytes_forward', ' Init_Win_bytes_backward', ' act_data_pkt_fwd', ' min_seg_size_forward', 'Active Mean', ' Active Std', ' Active Max', ' Active Min', 'Idle Mean', ' Idle Std', ' Idle Max', ' Idle Min', 'SimillarHTTP', ' Inbound', ' Label'], dtype='object')
The data in the label is
print("Label")
print(Syn[' Label'].unique())
Label ['Syn' 'BENIGN']
I want to change it to 1 and 0 so i can use it in logistic regression and other machine learring algorithms and deep learning. i tried the following two aproaches but cant seem to get it to work as i get errors on both methods as seen below
def change_label(cols):
Syn[' Label'] = cols[0]
if Syn[' Label']== 'Syn':
return 1
elif Syn[' Label'] == 'BENIGN':
return 0
Syn[' Label'] = Syn[[' Label']].apply(change_label,axis=1)
and the error i get is ValueError Traceback (most recent call last) in ----> 1 Syn[' Label'] = Syn[[' Label']].apply(change_label,axis=1)
~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in apply(self, func, axis,
broadcast, raw, reduce, result_type, args, **kwds)
6485 args=args,
6486 kwds=kwds)
-> 6487 return op.get_result()
6488
6489 def applymap(self, func):
~/anaconda3/lib/python3.7/site-packages/pandas/core/apply. 613