4

Is there any way to select the row by index (i.e. integer) and column by column name in a pandas data frame?

I tried using loc but it returns an error, and I understand iloc only works with indexes.

Here is the first rows of the data frame df. I am willing to select the first row, column named 'Volume' and tried using df.loc[0,'Volume']

enter image description here

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Ricardo Milhomem
  • 159
  • 2
  • 3
  • 11
  • You could provide the index using `df.iloc[0].index`. So the full thing becomes `df.loc[ df.iloc[0].index, 'Volume' ]` (note this is not an efficient solution) – pu239 Aug 05 '21 at 02:13
  • how about using `.iloc` then column name? `df.iloc[0]['Volume']` – sammy Aug 05 '21 at 02:16

1 Answers1

4

Use get_loc method of Index to get integer location of a column name.

Suppose this dataframe:

>>> df
    A  B  C
10  1  2  3
11  4  5  6
12  7  8  9

You can use .iloc like this:

>>> df.iloc[1, df.columns.get_loc('B')]
5
Corralien
  • 109,409
  • 8
  • 28
  • 52