1

How can I get exact row and column number i.e cell address by value in Pandas dataframe? For example, I have the following dataframe:

     ClientID  LastName
0    34        Johnson
1    67        Smith
2    53        Brows  

How can I find the cell address that has'Smith' as value? something which returns me [1,1] as value

I know the reverse is possible eg: df.get_value(1, 'LastName') but note that here I also do not know the column name. I just have the key value and want to find exact cell address of the dataframe.

1 Answers1

1

If use numpy.where without second and third parameetr it return positions of matched value, [0] is added for first match by index and by columns:

i, c = np.where(df == 'Smith')
val = [i[0], c[0]]
print (val)
[1, 1]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • This does not help to search content in dataframe header. I mean if I had to search ``` i,c = np.where(df == 'LastName') ``` it will not work. I am new to pandas, can you help me here? – aayush shah Jan 23 '20 at 12:12
  • @aayushshah - Sure, for check in columns use `i1, c1 = np.where(df.columns == 'Smith')`, I think `i1` is always same – jezrael Jan 23 '20 at 12:14