Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe select row by index and column by name

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

like image 479
Ricardo Milhomem Avatar asked Oct 27 '25 11:10

Ricardo Milhomem


1 Answers

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
like image 153
Corralien Avatar answered Oct 29 '25 01:10

Corralien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!