我想根据两列条件替换列'k',并在github上找到此代码片段。
这是代码段:
df.loc[(df['column1'] == some_value) & (df['column2'] == some_other_value), ['column_to_change']] = new_value
这是我使用的代码:
def merged_algo_A(cluster_array):
rows,labels = readCluster(cluster_array) # reading the test data
xtest = pd.DataFrame(np.array(rows).reshape(len(rows),11), columns = list("abcdefghijk"))
ytest = pd.DataFrame(np.array(labels).reshape(len(labels),1),columns = list("l"))
#print(xtrain['c'])
xtest['SportYes'] = np.where(xtest['c']<=16897, '200', '400')
xtest['stateYes'] = np.where(xtest['k']<=62, '200', '400')
xtest['durYes'] = np.where(xtest['a']<=0.0585, '200', '400')
xtest.loc[(xtest['SportYes'] == 200) & (xtest['stateYes'] == 200), 'k'] = 3
print(xtest)
答案 0 :(得分:0)
'200'
中的'400'
和np.where
需要更改为200
和400
才能获得整数:
xtest['SportYes'] = np.where(xtest['c']<=16897, 200, 400)
xtest['stateYes'] = np.where(xtest['k']<=62, 200, 400)
xtest['durYes'] = np.where(xtest['a']<=0.0585, 200, 400)
xtest.loc[(xtest['SportYes'] == 200) & (xtest['stateYes'] == 200), 'k'] = 3
print(xtest)
或者在''
语句中将200
添加到400
和loc
,以便string
进行比较:
xtest['SportYes'] = np.where(xtest['c']<=16897, '200', '400')
xtest['stateYes'] = np.where(xtest['k']<=62, '200', '400')
xtest['durYes'] = np.where(xtest['a']<=0.0585, '200', '400')
xtest.loc[(xtest['SportYes'] == '200') & (xtest['stateYes'] == '200'), 'k'] = 3
print(xtest)
答案 1 :(得分:0)
此问题已解决,以下是解决此问题的代码:
def merged_algo_A(cluster_array):
rows,labels = readCluster(cluster_array) # reading the test data
xtest = pd.DataFrame(np.array(rows).reshape(len(rows),11), columns = list("abcdefghijk"))
ytest = pd.DataFrame(np.array(labels).reshape(len(labels),1),columns = list("l"))
xtest['SportYes'] = np.where(xtest['c']<=16897, 'yes', 'no')
xtest['stateYes'] = np.where(xtest['f']<=62, 'yes', 'no')
xtest['labels1'] = xtest['j']
xtest['reallabels'] = ytest
xtest.loc[(xtest['SportYes'] == 'yes') , ['labels1']] = 3
xtest.loc[(xtest['SportYes'] == 'no') , ['labels1']] = 2
list1 = list(map(int,xtest['labels1']))
final_preds = list1
return final_preds