Get index value from pandas dataframe

14,201

call the index directly

return countries.index[2]

but what you post here looks like a pandas dataframe instead of a series - if that's the case do

countries['Country_Name'].iloc[2]
Share:
14,201

Related videos on Youtube

beshr
Author by

beshr

Updated on June 04, 2022

Comments

  • beshr
    beshr almost 2 years

    I have a Pandas dataframe (countries) and need to get specific index value. (Say index 2 => I need Japan)

    Series

    I used iloc, but i got the data (7.542)

    return countries.iloc[2]
    7.542
    
    • Umar.H
      Umar.H over 3 years
      you sure it's a series and not a dataframe?
    • Kaleb Coberly
      Kaleb Coberly over 3 years
      Maybe the confusion has to do with the fact that each column in a dataframe is a series. @beshr, are you operating on a column (or columns) in a dataframe, or just handling the dataframe directly?
    • questionto42standswithUkraine
      questionto42standswithUkraine almost 3 years
      Downvote. Since the edit queue is full: the header is very vague. You want to get the value from a chosen index of a df. One might read the header as if you search for the index value(s) for a given df value.
  • Kaleb Coberly
    Kaleb Coberly over 3 years
    the second solution didn't work on my dataframe, but the first solution did. The second solution works for accessing a value in a column. If you want the index of the value of a column, you would do countries['Country_Name'].index[2]