我要在csv文件上应用这两个条件
1.(Fwd数据包的总长度$ <= 0.00003225和Fwd数据包的总长度<= 0.000043 AND Fwd数据包的最大长度> 0.00021549999999999998)
2.(转发数据包最大长度<= 0.00021549999999999998)
如果这些条件成立,我想在“活动”列中写入攻击,然后将整个文件写入带有活动的新csv中
import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
import codecs
import csv
main_labels=["Total Length of Fwd Packets","Fwd Packet Length Max","Flow Bytes/s","Label","Activity"]
df = pd.read_csv('portscanfeatures.csv',usecols=main_labels)
attack_or_not=[]
for i in df["Fwd Packet Length Max"]:#it changes the normal label to "1" and
the attack tag to "0" for use in the machine learning algorithm
if i <= 0.00021549999999999998:
attack_or_not.append(1)
else:
attack_or_not.append(0)
答案 0 :(得分:0)
这有效。只需列出条件,然后使用numpy.where放置所需的值即可。
import numpy as np
conditions = ((df['Total Length of Fwd Packets'] <= 0.00003225) & (df['Total Length of Fwd Packets'] <= 0.000043) & (df['Fwd Packet Length Max'] > 0.00021549999999999998) & (df['Fwd Packet Length Max'] <=0.00021549999999999998))
df['attack'] = np.where(conditions, 'Attack', "Don't")
#or if you want 1, 0 directly
df['attack'] = np.where(conditions, 1, 0)