How to get value from Pandas DataFrame cell
We'll look at using to get values from cells in iloc
Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .DataFrame
loc
iat
['col_name'].values[]
pandas.Series
iloc
DataFrame
Get value from cell in Pandas
iloc is the most efficient way to get values from a cell in Pandas dataframe. Suppose, we have a DataFrame
dataframe with columns named price
and stock
and want to get a value from the third row to check the price and stock availability.
First, we need to access the rows and then access the values using the column names.
Sample code:
# python 3.x
import pandas as pd
df = pd.DataFrame(
{
"name": ["orange", "banana", "lemon", "mango", "apple"],
"price": [2, 3, 7, 21, 11],
"stock": ["Yes", "No", "Yes", "No", "Yes"],
}
)
print(df.iloc[2]["price"])
print(df.iloc[2]["stock"])
Output:
7
Yes
iloc
Get the row (or column) at a specific position in the index. That's why it only takes integers as parameters. Then loc
get the row (or column) with a specific label from the index.
Get values from cells of a Pandas DataFrame using iat
andat
iat
and at
are fast scalar access methods to DataFrame
get values from cells in Pandas.
Sample code:
# python 3.x
import pandas as pd
df = pd.DataFrame(
{
"name": ["orange", "banana", "lemon", "mango", "apple"],
"price": [2, 3, 7, 21, 11],
"stock": ["Yes", "No", "Yes", "No", "Yes"],
}
)
print(df.iat[0, 0])
print(df.at[1, "stock"])
Output:
orange
No
To get the last row of entries, we will use at[df.index[-1],'stock']
.
Sample code:
# python 3.x
import pandas as pd
df = pd.DataFrame(
{
"name": ["orange", "banana", "lemon", "mango", "apple"],
"price": [2, 3, 7, 21, 11],
"stock": ["Yes", "No", "Yes", "No", "Yes"],
}
)
print(df.at[df.index[-1], "stock"])
Output:
Yes
df['col_name'].values[]
Get Values from Cells in a Pandas DataFrame
df['col_name'].values[]
First datafarme
convert the column to a one-dimensional array, then access the value at the index of that array:
Sample code:
# python 3.x
import pandas as pd
df = pd.DataFrame(
{
"name": ["orange", "banana", "lemon", "mango", "apple"],
"price": [2, 3, 7, 21, 11],
"stock": ["Yes", "No", "Yes", "No", "Yes"],
}
)
print(df["stock"].values[0])
Output:
Yes
It returns nothing pandas.Series
, and is the simplest to use.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:161 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a
How to count the frequency of values in a Pandas DataFrame
Publish Date:2025/05/02 Views:84 Category:Python
-
Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().
How to set values for specific cells in a Pandas DataFrame using index
Publish Date:2025/05/02 Views:118 Category:Python
-
Pandas is a data-centric python package that makes data analysis in python easy and consistent. In this article, we will look at different ways to access and set specific cell values in a pandas DataFrame data structure using indexing
Convert Pandas to CSV without index
Publish Date:2025/05/01 Views:159 Category:Python
-
As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates
Convert Pandas DataFrame to Dictionary
Publish Date:2025/05/01 Views:198 Category:Python
-
This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan
Convert Pandas DataFrame columns to lists
Publish Date:2025/05/01 Views:192 Category:Python
-
When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m
Subtracting Two Columns in Pandas DataFrame
Publish Date:2025/05/01 Views:120 Category:Python
-
Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi
Dropping columns by index in Pandas DataFrame
Publish Date:2025/05/01 Views:99 Category:Python
-
DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns
Pandas Copy DataFrame
Publish Date:2025/05/01 Views:53 Category:Python
-
This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output: