1

I try to separate words form DataFrame Columns:

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

we want to change these column value into: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

   attack
0       1
1       0
2       0
3       1
4       0

only normal = 1 else 0

jose praveen
  • 1,298
  • 2
  • 10
  • 17
a zEnItH
  • 137
  • 3
  • 11

2 Answers2

1

Convert boolean mask to integers for True/False to 1/0 mapping:

data['attack'] = data['attack'].eq('normal').astype(int)

Or use numpy.where with specify both values:

data['attack'] = np.where(data['attack'].eq('normal'), 1, 0)

print(data)
  attack
0       1
1       0
2       0
3       1
4       0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1
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")
Rajesh
  • 63
  • 6