6

Pandas: Set value of a Cell in Dataframe

 2 years ago
source link: https://thispointer.com/pandas-set-value-of-a-cell-in-dataframe/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Pandas: Set value of a Cell in Dataframe

This article will discuss different ways to set the value of a cell in a Pandas Dataframe in Python.

Table of Contents:

First of all, we will create a Dataframe from a list of columns,

import pandas as pd
# List of Tuples
students = [('jack', 34, 'Sydeny', 'Australia'),
('Riti', 30, 'Delhi', 'France'),
('Vikas', 31, 'Mumbai', 'India'),
('Neelu', 32, 'Bangalore', 'Germany'),
('John', 16, 'New York', 'US'),
('Mike', 17, 'las vegas', 'US')]
# Create a DataFrame from list of tuples
df = pd.DataFrame( students,
columns=['Name', 'Age', 'City', 'Country'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
print(df)
import pandas as pd

# List of Tuples
students = [('jack',  34, 'Sydeny',    'Australia'),
            ('Riti',  30, 'Delhi',     'France'),
            ('Vikas', 31, 'Mumbai',    'India'),
            ('Neelu', 32, 'Bangalore', 'Germany'),
            ('John',  16, 'New York',  'US'),
            ('Mike',  17, 'las vegas', 'US')]

# Create a DataFrame from list of tuples
df = pd.DataFrame( students,
                   columns=['Name', 'Age', 'City', 'Country'],
                   index=['a', 'b', 'c', 'd', 'e', 'f'])

print(df)

Contents of this Dataframe are as follows,

Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi France
c Vikas 31 Mumbai India
d Neelu 32 Bangalore Germany
e John 16 New York US
f Mike 17 las vegas US
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi     France
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore    Germany
e   John   16   New York         US
f   Mike   17  las vegas         US

Now we will explore different techniques to change the value of a cell in this dataframe using label names or index positions or conditions.

Set Value of a Cell in Pandas Dataframe using row/column numbers

First, we need to select the cell from Dataframe using its index positions, i.e. its row and column number. Then we can update its value. An important point to remember is that indexing starts from zero. It means the index position/number of the Nth row or column will be N-1. For example,

Looking for a Career in Data Science or Machine Learning with Python?

Get a Professional Certificate in data Science by IBM. Build data science skills, learn Python & SQL, analyze & visualize data, build machine learning models. No degree or prior experience required. Checkout the detailed review.

Explore a new career path with a 7 day free trial.

  • 3rd row of the Dataframe is row number 2
  • 4th Column of the Dataframe is column number 3.

To set the cell value by row/column number, we have different techniques i.e, either using Dataframe.iat[] or Dataframe.iloc[]. Let’s discuss them one by one,

Set value of a Cell in Dataframe using iat[]

In Pandas, the Dataframe provides an attribute iat[] to access a single cell value, based on its row & column numbers. We can select the cell and then update its value. For example, let’s set the value of a cell at 3rd row and 2nd column in the dataframe,

row_num = 2
col_num = 1
# Set Cell Value at 3rd row and 2nd column
# (Index positions starts from 0)
df.iat[row_num , col_num] = 100
print(df)
row_num = 2
col_num = 1

# Set Cell Value at 3rd row and 2nd column
# (Index positions starts from 0)
df.iat[row_num , col_num] = 100

print(df)

Output:

Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi France
c Vikas 100 Mumbai India
d Neelu 32 Bangalore Germany
e John 16 New York US
f Mike 17 las vegas US
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi     France
c  Vikas  100     Mumbai      India
d  Neelu   32  Bangalore    Germany
e   John   16   New York         US
f   Mike   17  las vegas         US

It updated the cell value in the 3rd row and 2nd column in Dataframe to 100.

Some Important Point:

  • As Row and Column numbers start from 0 in DataFrame, row number 2 points to the third row of dataframe, and column number 1 points to the second column of DataFrame.
  • If any of the given index positions/numbers in iat[] is out of bounds, it can give IndexError.

Set Cell value in Pandas Dataframe using iloc[]

In Pandas, the Dataframe provides a property iloc[], to select the subset of Dataframe based on position indexing. Area of the subset will be decided based on the provided index positions/numbers of rows & columns. Although we can select single or multiple rows & columns using it. But today, we will choose a single cell using it and then update its value. For example, let’s set the cell value at the 3rd row and 2nd column of the Dataframe using iloc[]

row_num = 2
col_num = 1
# Set Cell Value at 3rd row and 2nd column
# (Index positions starts from 0)
df.iloc[row_num , col_num] = 55
print(df)
row_num = 2
col_num = 1

# Set Cell Value at 3rd row and 2nd column
# (Index positions starts from 0)
df.iloc[row_num , col_num] = 55

print(df)

Output:

Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi France
c Vikas 55 Mumbai India
d Neelu 32 Bangalore Germany
e John 16 New York US
f Mike 17 las vegas US
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi     France
c  Vikas   55     Mumbai      India
d  Neelu   32  Bangalore    Germany
e   John   16   New York         US
f   Mike   17  las vegas         US

It updated the cell value at the 3rd row and 2nd column of the DataFrame.

Important Point:

As indexing starts from 0 in DataFrame, the index position of the 3rd row is 2, and for the 2nd column, it is 1.

Set Cell Value of a Pandas Dataframe using row & column labels/names

We can set the value of a cell in Dataframe based on row and column names using loc[] and at[] attributes. Let’s discuss them one by one.

Set cell value using at[] in Pandas Dataframe

In Pandas, the DataFrame provides a property at[], to select a single cell from a Dataframe by row and column label names. After selecting, we can update the value of that cell. The syntax is as follows,

DataFrame.at[row_label, column_name] = new_value
DataFrame.at[row_label, column_name] = new_value

For example, let’s set the cell value at row ‘c’ and column ‘Age’ of the Dataframe using iloc[]

row_label = 'c'
column_name = 'Age'
# Set cell value at row 'c' and column 'Age'
df.at[row_label, column_name] = 78
print(df)
row_label   = 'c'
column_name = 'Age'

# Set cell value at row 'c' and column 'Age'
df.at[row_label, column_name] = 78

print(df)

Output:

Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi France
c Vikas 78 Mumbai India
d Neelu 32 Bangalore Germany
e John 16 New York US
f Mike 17 las vegas US
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi     France
c  Vikas   78     Mumbai      India
d  Neelu   32  Bangalore    Germany
e   John   16   New York         US
f   Mike   17  las vegas         US

It updated the cell value at row ‘c’ and column ‘Age’ of the DataFrame.

Set cell value using loc[] in Pandas Dataframe

In Pandas, the Dataframe provides a property loc[], to select the subset of Dataframe based on row and column names/labels. Although, we can choose single or multiple rows & columns using it. But today, we will select a single cell using it, and then we will update its value with the following syntax,

pandas.DataFrame.at[row_label , column_name] = new_value
pandas.DataFrame.at[row_label , column_name] = new_value

We will Set the value of a single cell using it. For example, let’s Set cell value at row ‘c’ and column ‘Age’ of the DataFrame,

row_label = 'c'
column_name = 'Age'
# Set cell value at row 'c' and column 'Age'
df.loc[row_label, column_name] = 79
print (df)
row_label   = 'c'
column_name = 'Age'

# Set cell value at row 'c' and column 'Age'
df.loc[row_label, column_name] = 79

print (df)

Output:

Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi France
c Vikas 79 Mumbai India
d Neelu 32 Bangalore Germany
e John 16 New York US
f Mike 17 las vegas US
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi     France
c  Vikas   79     Mumbai      India
d  Neelu   32  Bangalore    Germany
e   John   16   New York         US
f   Mike   17  las vegas         US

It updated the value of the cell at row ‘c’ and column ‘Age’ of the DataFrame.

Pandas: Set cell value based on condition

We can set the value of a cell in Dataframe based on conditions on other columns. For example, Set cell values in column ‘Name’, where column ‘Age’ is 32,

# Set cell value of column 'Name', where column 'Age' is 32
df.loc[df['Age'] == 32, 'Name'] = 'Shyam'
print (df)
# Set cell value of column 'Name', where column 'Age' is 32
df.loc[df['Age'] == 32, 'Name'] = 'Shyam'

print (df)

Output:

Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi France
c Vikas 79 Mumbai India
d Shyam 32 Bangalore Germany
e John 16 New York US
f Mike 17 las vegas US
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi     France
c  Vikas   79     Mumbai      India
d  Shyam   32  Bangalore    Germany
e   John   16   New York         US
f   Mike   17  las vegas         US

Using df[df[‘Age’] == 32] it selected only those rows where column ‘Age’ has value 32. Then, it fetched column ‘Name’ values and then updated their values.

Set value of the first cell of a Column

To Set the value of the first cell of any column, we first need to select the column as a Series object and then update the first entry. For example, let’s update the value of the first cell in column ‘Age’ of the DataFrame,

# Set value of first cell of Column 'Age'
df['Age'].values[0] = 100
print(df)
# Set value of first cell of Column 'Age' 
df['Age'].values[0] = 100

print(df)

Output:

Name Age City Country
a jack 100 Sydeny Australia
b Riti 30 Delhi France
c Vikas 79 Mumbai India
d Shyam 32 Bangalore Germany
e John 16 New York US
f Mike 17 las vegas US
    Name  Age       City    Country
a   jack  100     Sydeny  Australia
b   Riti   30      Delhi     France
c  Vikas   79     Mumbai      India
d  Shyam   32  Bangalore    Germany
e   John   16   New York         US
f   Mike   17  las vegas         US

It updated the value of the first cell of column ‘Age’.

Summary

Today we learned about different techniques to set the value of a cell in a Pandas Dataframe in Python.

Advertisements


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK