How to get the content of a cell in a pandas dataframe

Viewed 385

I want to be able to take out one value in the table and reference it somewhere else in my code but I can only get the print statement to print a column.

Animals.csv {
Animal, Name, Age
Dog, Albert, 2
Cat, John, 8
Monkey, Ege, 3
}

Here is my code:

import pandas as pd

data = pd.read_csv("Animals.csv")

print(data["Monkey"]["Name"]) 

and it should print out Ege (the content of cell, instead of a full column, with index)

2 Answers

What about :

print(data["Monkey"]["Name"].values[0])

As previously, you extract the row/column you want, based on the row's index and column name. Then .values turn the result dataframe into a numpy arrays of values. Finally, just retrieve the first (and only) element

You might also be able to use :

print(data["Monkey"].iloc[0]["Name"])

First you filter the dataframe by the condition that Animal is a Monkey. This will return row 2. Then you index that row by Name:

data = pd.read_csv("Animals.csv")
result = data[data['Animal'] == 'Monkey']['Name']

print(result)
#2    Ege
Related