我尝试从DataFrame列中分离单词:
Binary = []
for i in range(0,len(data)):
if data['attack'][i] == 'normal':
Binary.append(1)
else:
Binary.append(0)
print(len(Binary))
data= pd.DataFrame({'attack':['normal','neptune','ps','normal','neptune']})
print(data)
attack
0 normal
1 neptune
2 ps
3 normal
4 neptune
我们要将这些列值更改为: ValueError:系列的真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
attack
0 1
1 0
2 0
3 1
4 0
仅正常= 1
否则0
答案 0 :(得分:0)
将布尔掩码转换为整数,以实现True/False
到1/0
的映射:
data['attack'] = data['attack'].eq('normal').astype(int)
或使用numpy.where
同时指定两个值:
data['attack'] = np.where(data['attack'].eq('normal'), 1, 0)
print(data)
attack
0 1
1 0
2 0
3 1
4 0
答案 1 :(得分:0)
for index, row in data.iteritems():
for each in row.iteritems():
if each[1] is "normal":
print(each[0], "0")
else:
print(each[0], "1")