JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

How to get value from Pandas DataFrame cell

Author:JIYIK Last Updated:2025/05/02 Views:

We'll look at using to get values ​​from cells in ilocPandas , 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 .DataFramelociat['col_name'].values[]pandas.Series


ilocDataFrameGet value from cell in Pandas

iloc is the most efficient way to get values ​​from a cell in Pandas dataframe. Suppose, we have a DataFramedataframe with columns named priceand stockand 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

ilocGet the row (or column) at a specific position in the index. That's why it only takes integers as parameters. Then locget the row (or column) with a specific label from the index.


Get values ​​from cells of a Pandas DataFrame using iatandat

iatand atare fast scalar access methods to DataFrameget 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 datafarmeconvert 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.

Article URL:

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().

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:

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial