In last column selected by columns name with indexing with DataFrame.loc
, there are all values 4
because in input data are 1
- c = np.ones(8)
:
#working well for unique columns names
e.loc[(e.iloc[:,-1]<1.01) & (e.iloc[:,-1]>0.99), e.columns[-1]]=4.0
Or by DataFrame.iloc
with convert mask to boolean array:
#working well for any columns names
e.iloc[((e.iloc[:,-1]<1.01) & (e.iloc[:,-1]>0.99)).to_numpy(), -1]=4.0
print (e)
0 1 2
0 0.0 10.0 4.0
1 1.0 11.0 4.0
2 2.0 12.0 4.0
3 3.0 13.0 4.0
4 4.0 14.0 4.0
5 5.0 15.0 4.0
6 6.0 16.0 4.0
7 7.0 17.0 4.0
Changed data sample:
np.random.seed(2022)
a = np.arange(0,8,1)
b = np.arange(10,18,1)
c = np.random.random(8) + 0.5
d = np.column_stack((a,b,c))
e = pd.DataFrame(d)
print (e)
0 1 2
0 0.0 10.0 0.509359
1 1.0 11.0 0.999058
2 2.0 12.0 0.613384
3 3.0 13.0 0.549974
4 4.0 14.0 1.185408
5 5.0 15.0 0.986988
6 6.0 16.0 1.397657
7 7.0 17.0 1.147452
e.iloc[((e.iloc[:,-1]<1.01) & (e.iloc[:,-1]>0.99)).to_numpy(), -1]=4.0
print (e)
0 1 2
0 0.0 10.0 0.509359
1 1.0 11.0 4.000000
2 2.0 12.0 0.613384
3 3.0 13.0 0.549974
4 4.0 14.0 1.185408
5 5.0 15.0 0.986988
6 6.0 16.0 1.397657
7 7.0 17.0 1.147452