My input:
JavaScript
x
8
1
import pandas as pd
2
dd = pd.DataFrame({'frame':[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
3
'sum_result_ICV':[0,1,1,1,2,2,2,2,1,1,1,1,1,1,1,0],
4
'sum_result_ATO':[0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0]})
5
dd['result_ICV'] = 0
6
dd['result_ATO'] = 0
7
cv_detail = pd.Series(['ICV','ATO'])
8
I try assign new value based on other column in this way it work:
dd.loc[dd.iloc[:,1]==0,['result_'+cv_detail[0]] ] = 'Other'
OR dd.loc[dd.sum_result_ICV == 0, "result_ICV"] = 'Other'
But this code doesn’t, code make more columns, with assign new value into column:
JavaScript
1
2
1
dd.loc[dd.iloc[:,1]==0, dd.iloc[:,3]] = 'Other'
2
What difference?
Advertisement
Answer
Use double iloc
first for select second column by dd.iloc[:,1]
, conmvert to numpy array for possible assign in another iloc
for fourth column (3)
:
JavaScript
1
21
21
1
dd.iloc[dd.iloc[:,1].to_numpy() == 0, 3] = 'Other'
2
3
print (dd)
4
frame sum_result_ICV sum_result_ATO result_ICV result_ATO
5
0 0 0 0 Other 0
6
1 1 1 1 0 0
7
2 2 1 1 0 0
8
3 3 1 1 0 0
9
4 4 2 0 0 0
10
5 5 2 0 0 0
11
6 6 2 0 0 0
12
7 7 2 0 0 0
13
8 8 1 1 0 0
14
9 9 1 1 0 0
15
10 10 1 1 0 0
16
11 11 1 1 0 0
17
12 12 1 1 0 0
18
13 13 1 1 0 0
19
14 14 1 1 0 0
20
15 15 0 0 Other 0
21