简体   繁体   中英

How to change pandas column values based on condition?

I have pandas dataframe like

import numpy as np
import pandas as pd
a = np.arange(0,8,1)
b = np.arange(10,18,1)
c = np.ones(8)
d = np.column_stack((a,b,c))
e = pd.dataframe(d)
e.loc[(e.iloc[:,-1]<1.01) & (e.iloc[:,-1]>0.99)]=4.0

I want to change last column of e to 4.0 wherever values are <1.01 & >0.99 . However, the last line changes all values to 4.0 . I tried to follow https://stackoverflow.com/a/15315507 above, but looks like not working. Any idea what is wrong?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM