Skip to content
Advertisement

Index Pandas Dataframe mixing row number and column name

Coming from R and finding the index rules for pandas dataframes to be not easy to use. I have a dataframe where I want to get the ith row and some columns by their names. I can clearly understand using either iloc or loc as shown below.

JavaScript

Conceptually what I want is something like:

JavaScript

Meaning the first row with those columns. Of course that code fails. I can seemingly use:

JavaScript

But, this seems strange, though it works. How does one properly index using a combination of row number and column names? In R we would do something like:

JavaScript

*** UPDATE *** I was mistaken, the example code above indeed works on this toy dataframe. But, on my real data, I see the following? Both objects are of same type and code is the same, not understanding the error here.

JavaScript

Advertisement

Answer

You are using slicing which means between two given index. If you only want first row data just use:

Try:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement