Home > other >  If based on specific values in pandas df
If based on specific values in pandas df

Time:07-02

I have a pandas dataframe such as this :

np.random.seed(0)
df = pd.DataFrame(np.random.randn(5,3), columns=list('ABC'))
df['reject'] = ['RHO','AHO','AHO','RHO','RHO']

          A         B         C reject
0  1.764052  0.400157  0.978738    RHO
1  2.240893  1.867558 -0.977278    AHO
2  0.950088 -0.151357 -0.103219    AHO
3  0.410599  0.144044  1.454274    RHO
4  0.761038  0.121675  0.443863    RHO

And i'd like to compute based on last row of the last column. I try it doing so :

if 'AHO' in df.iloc[-1:,-1:]:
   print('AH0')
elif 'RHO' in df.iloc[-1:,-1:]:
   print('RH0')

But my conditions does not match any of the values of the last cell of my dataframe. I think that i do not have the right way to select this cell but can not find the proper way.

Can anyone help me ?

CodePudding user response:

You don't need slice

if 'AHO' in df.iloc[-1,-1]:
   print('AH0')
elif 'RHO' in df.iloc[-1,-1]:
   print('RH0')
  • Related