Python get cell value

Pandas how to get a cell value and update it

Accessing a single value or setting up the value of single row is sometime required when we doesn’t want to create a new Dataframe for just updating that single cell value. There are indexing and slicing methods available but to access a single cell values there are Pandas in-built functions at and iat.

Читайте также:  Css import url fonts

Since indexing with [] must handle a lot of cases (single-label access, slicing, boolean indexing, etc.), it has a bit of overhead in order to figure out what you’re asking for. If you only want to access a scalar value, the fastest way is to use the at and iat methods, which are implemented on all of the data structures.

Similarly to loc, at provides label based scalar lookups, while, iat provides integer based lookups analogously to iloc

Found a very Good explanation in one of the StackOverflow Answers which I wanted to Quote here:

There are two primary ways that pandas makes selections from a DataFrame.

By Label By Integer Location

There are three primary indexers for pandas. We have the indexing operator itself (the brackets [] ), .loc , and .iloc . Let’s summarize them:

[] — Primarily selects subsets of columns, but can select rows as well. Cannot simultaneously select rows and columns. .loc — selects subsets of rows and columns by label only .iloc — selects subsets of rows and columns by integer location only

Never used .at or .iat as they add no additional functionality and with just a small performance increase. I would discourage their use unless you have a very time-sensitive application. Regardless, we have their summary:

.at selects a single scalar value in the DataFrame by label only .iat selects a single scalar value in the DataFrame by integer location only

In addition to selection by label and integer location, boolean selection also known as boolean indexing exists.

Dataframe cell value by Column Label

at — Access a single value for a row/column label pair Use at if you only need to get or set a single value in a DataFrame or Series.

Let’s create a Dataframe first

import pandas as pd df = pd.DataFrame([[30, 20, 'Hello'], [None, 50, 'foo'], [10, 30, 'poo']], columns=['A', 'B', 'C']) df 

Let’s access cell value of (2,1) i.e index 2 and Column B

Value 30 is the output when you execute the above line of code

Now let’s update the only NaN value in this dataframe to 50 , which is located at cell 1,1 i,e Index 1 and Column A

So you have seen how we have updated the cell value without actually creating a new Dataframe here

Let’s see how do you access the cell value using loc and at

df.loc[1].B OR df.loc[1].at['B'] Output: 50 

Dataframe cell value by Integer position

From the above dataframe, Let’s access the cell value of 1,2 i.e Index 1 and Column 2 i.e Col C

iat — Access a single value for a row/column pair by integer position. Use iat if you only need to get or set a single value in a DataFrame or Series.

Let’s setup the cell value with the integer position, So we will update the same cell value with NaN i.e. cell(1,0)

Select rows in a MultiIndex Dataframe

Pandas xs Extract a particular cross section from a Series/DataFrame. This method takes a key argument to select data at a particular level of a MultiIndex.

Let’s create a multiindex dataframe first

#xs import itertools import pandas as pd import numpy as np a = ('A', 'B') i = (0, 1, 2) b = (True, False) idx = pd.MultiIndex.from_tuples(list(itertools.product(a, i, b)), names=('Alpha', 'Int', 'Bool')) df = pd.DataFrame(np.random.randn(len(idx), 7), index=idx, columns=('I', 'II', 'III', 'IV', 'V', 'VI', 'VII')) 

Access Alpha = B

Access Alpha = ‘B’ and Bool == False

df.xs(('B', False), level=('Alpha', 'Bool')) 

Access Alpha = ‘B’ and Bool == False and Column III

df.xs(('B', False), level=('Alpha', 'Bool'))['III'] 

So you have seen how you can access a cell value and update it using at and iat which is meant to access a scalar, that is, a single element in the dataframe, while loc and iloc are meant to access several elements at the same time, potentially to perform vectorized operations. at Works very similar to loc for scalar indexers. Cannot operate on array indexers.Advantage over loc is that this is faster. Similarly, iat Works similarly to iloc but both of them only selects a single scalar value. Further to this you can read this blog on how to update the row and column values based on conditions.

Updated: April 12, 2019

Share on

You may also enjoy

pandas count duplicate rows

DataFrames are a powerful tool for working with data in Python, and Pandas provides a number of ways to count duplicate rows in a DataFrame. In this article.

Pandas value error while merging two dataframes with different data types

If you’re encountering a “value error” while merging Pandas data frames, this article has got you covered. Learn how to troubleshoot and solve common issues .

How to get True Positive, False Positive, True Negative and False Negative from confusion matrix in scikit learn

In machine learning, we often use classification models to predict the class labels of a set of samples. The predicted labels may or may not match the true .

Pandas how to use list of values to select rows from a dataframe

In this post we will see how to use a list of values to select rows from a pandas dataframe We will follow these steps to select rows based on list of value.

Источник

Pandas – How to Get Cell Value From DataFrame?

You can use DataFrame properties loc[] , iloc[] , at[] , iat[] and other ways to get/select a cell value from a Pandas DataFrame. Pandas DataFrame is structured as rows & columns like a table, and a cell is referred to as a basic block that stores the data. Each cell contains information relating to the combination of the row and column.

1. Quick Examples of Get Cell Value of DataFrame

If you are in a hurry, below are some of the quick examples of how to select cell values from pandas DataFrame.

 # Below are some quick examples # Using loc[]. Get cell value by name & index print(df.loc['r4']['Duration']) print(df.loc['r4'][2]) # Using iloc[]. Get cell value by index & name print(df.iloc[3]['Duration']) print(df.iloc[3,2]) # Using DataFrame.at[] print(df.at['r4','Duration']) print(df.at[df.index[3],'Duration']) # Using DataFrame.iat[] print(df.iat[3,2]) # Get a cell value print(df["Duration"].values[3]) # Get cell value from last row print(df.iloc[-1,2]) print(df.iloc[-1]['Duration']) print(df.at[df.index[-1],'Duration']) 

Now, let’s create a DataFrame with a few rows and columns and execute some examples and validate the results. Our DataFrame contains column names Courses , Fee , Duration , Discount .

 # Create DataFrame import pandas as pd technologies = < 'Courses':["Spark","PySpark","Hadoop","Python","pandas"], 'Fee' :[24000,25000,25000,24000,24000], 'Duration':['30day','50days','55days', '40days','60days'], 'Discount':[1000,2300,1000,1200,2500] >index_labels=['r1','r2','r3','r4','r5'] df = pd.DataFrame(technologies, index=index_labels) print(df) 
 # Output: Courses Fee Duration Discount r1 Spark 24000 30day 1000 r2 PySpark 25000 50days 2300 r3 Hadoop 25000 55days 1000 r4 Python 24000 40days 1200 r5 pandas 24000 60days 2500 

2. Using DataFrame.loc[] to Get a Cell Value by Column Name

In Pandas, DataFrame.loc[] property is used to get a specific cell value by row & label name(column name). Below all examples return a cell value from the row label r4 and Duration column (3rd column).

 # Using loc[]. Get cell value by name & index print(df.loc['r4']['Duration']) print(df.loc['r4','Duration']) print(df.loc['r4'][2]) 

Yields below output. From the above examples df.loc[‘r4’] returns a pandas Series.

3. Using DataFrame.iloc[] to Get a Cell Value by Column Position

If you wanted to get a cell value by column number or index position use DataFrame.iloc[] , index position starts from 0 to length-1 (index starts from zero). In order to refer last column use -1 as the column position.

 # Using iloc[]. Get cell value by index & name print(df.iloc[3]['Duration']) print(df.iloc[3][2]) print(df.iloc[3,2]) 

This returns the same output as above. Note that iloc[] property doesn’t support df.iloc[3,’Duration’] , by using this notation, returns an error.

4. Using DataFrame.at[] to select Specific Cell Value by Column Label Name

DataFrame.at[] property is used to access a single cell by row and column label pair. Like loc[] this doesn’t support column by position. This performs better when you wanted to get a specific cell value from Pandas DataFrame as it uses both row and column labels. Note that at[] property doesn’t support negative index to refer rows or columns from last.

 # Using DataFrame.at[] print(df.at['r4','Duration']) print(df.at[df.index[3],'Duration']) 

These examples also yield the same output 40days .

5.Using DataFrame.iat[] select Specific Cell Value by Column Position

DataFrame.iat[] is another property to select a specific cell value by row and column position. Using this you can refer to column only by position but not by a label. This also doesn’t support negative index or column position.

 # Using DataFrame.iat[] print(df.iat[3,2]) 

6. Select Cell Value from DataFrame Using df[‘col_name’].values[]

We can use df[‘col_name’].values[] to get 1×1 DataFrame as a NumPy array, then access the first and only value of that array to get a cell value, for instance, df[«Duration»].values[3] .

 # Get a cell value print(df["Duration"].values[3]) 

7. Get Cell Value from Last Row of Pandas DataFrame

If you wanted to get a specific cell value from the last Row of Pandas DataFrame, use the negative index to point the rows from last. For example, Index -1 represents the last row and -2 for the second row from the last. Similarly, you should also use -1 for the last column.

 # Get cell value from last row print(df.iloc[-1,2]) # prints 60days print(df.iloc[-1]['Duration']) # prints 60days print(df.at[df.index[-1],'Duration']) # prints 60days 

To select the cell value of the last row and last column use df.iloc[-1,-1] , this returns 2500 . Similarly, you can also try other approaches.

8. Conclusion

In this article, you have learned how to get or select a specific cell value from pandas DataFrame using the .iloc[] , .loc[] , .iat[] & .at[] properties. Also, you have learned how to get a specific value from the last row and last row, last column with examples.

References

You may also like reading:

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply Cancel reply

This Post Has One Comment

Thanks Larsen for pointing it out. You are right and I have corrected it.

Источник

Get a Value From a Cell of a Pandas DataFrame

Get a Value From a Cell of a Pandas DataFrame

  1. iloc to Get Value From a Cell of a Pandas DataFrame
  2. iat and at to Get Value From a Cell of a Pandas DataFrame
  3. df[‘col_name’].values[] to Get Value From a Cell of a Pandas Dataframe

We will introduce methods to get the value of a cell in Pandas DataFrame . They include iloc and iat . [‘col_name’].values[] is also a solution especially if we don’t want to get the return type as pandas.Series .

iloc to Get Value From a Cell of a Pandas DataFrame

iloc is the most efficient way to get a value from the cell of a Pandas DataFrame . Suppose we have a DataFrame with the columns’ names as price and stock , and we want to get a value from the 3rd row to check the price and stock availability.

First, we need to access rows and then the value using the column name.

# 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']) 

iloc gets rows (or columns) at particular positions in the index. That’s why it only takes an integer as the argument. And loc gets rows (or columns) with the given labels from the index.

iat and at to Get Value From a Cell of a Pandas DataFrame

iat and at are fast accesses for scalars to get the value from a cell of a Pandas DataFrame .

# 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']) 

To get the last row entry, we will use at[df.index[-1],’stock’] .

# 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']) 

df[‘col_name’].values[] to Get Value From a Cell of a Pandas Dataframe

df[‘col_name’].values[] will first convert datafarme column into 1-D array then access the value at index of that array:

# 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]) 

It does not return a pandas.Series , and it’s the simplest to use.

Related Article — Pandas DataFrame

Copyright © 2023. All right reserved

Источник

Оцените статью