1

I have identified the row numbers of where I need to get within my excel file, with python pandas; using the below:

 row_numberd1 = df[df['Member Name'].str.contains(NameUp)].index.min()
 row_numberd12 = df[df['Member Address Line 3'].str.contains(Zip)].index.min()

i.e. when I print, the row numbers display:

 print(row_numberd1)
 print(row_numberd12)

Now, I am simply wondering how I can use the row number; to get a different columns value. So, get columns value by row number.

So, for instance, in the below sample:

   Age      City
1   34    Sydney
2   30     Delhi
3   16  New York

How could I use the row number; i.e. let's say Row 3, Column 'City' to get the 'New York' value?

Dr Upvote
  • 8,023
  • 24
  • 91
  • 204

2 Answers2

4

.loc for label based indexing

df.loc[3, 'City']
#'New_York'

# For a single value use `.at`
df.at[3, 'City']
#'New_York'

iloc for index based selection

df.iloc[2, 1]
#'New_York'

# For a single value use `.iat`
df.iat[2, 1]
#'New_York'

iloc + get_loc for a combination of both

(i.e. 3rd value in 'City')

df.iloc[2, df.columns.get_loc('City')]
#'New_York'
Community
  • 1
  • 1
ALollz
  • 57,915
  • 7
  • 66
  • 89
1

you can use df[column_name][row_number]

Moshee
  • 544
  • 1
  • 3
  • 16
  • 2
    This is called ["chained indexing"](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy) and should be avoided. – user3483203 Jul 29 '19 at 18:06