Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the content of a cell in a pandas dataframe [duplicate]

Tags:

python

pandas

csv

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)

like image 416
ChristopherOjo Avatar asked Dec 19 '25 10:12

ChristopherOjo


1 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"])
like image 158
AlexTorx Avatar answered Dec 20 '25 22:12

AlexTorx



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!