Lieutenant Dan
Lieutenant Dan

Reputation: 8264

Get value of column content by row with Python/Pandas

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?

Upvotes: 1

Views: 4293

Answers (2)

ALollz
ALollz

Reputation: 59509

.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'

Upvotes: 4

Moshee
Moshee

Reputation: 554

you can use df[column_name][row_number]

Upvotes: 1

Related Questions