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)
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"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With