[英]Try to separate words from pandas DataFrame column
我嘗試從 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
將布爾掩碼轉換為整數以實現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
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.