How to set values for specific cells in a Pandas DataFrame using index
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.
Use pandas.dataframe.at
the method to set values for specific cells in a pandas DataFrame
The method is mainly used when you need to set a single value in a DataFrame pandas.dataframe.at
.
import pandas as pd
sample_df = pd.DataFrame(
[[10, 20, 30], [11, 21, 31], [15, 25, 35]],
index=[0, 1, 2],
columns=["Col1", "Col2", "Col3"],
)
print "\nOriginal DataFrame"
print (pd.DataFrame(sample_df))
sample_df.at[0, "Col1"] = 99
sample_df.at[1, "Col2"] = 99
sample_df.at[2, "Col3"] = 99
print "\nModified DataFrame"
print (pd.DataFrame(sample_df))
Output:
Original DataFrame
Col1 Col2 Col3
0 10 20 30
1 11 21 31
2 15 25 35
Modified DataFrame
Col1 Col2 Col3
0 99 20 30
1 11 99 31
2 15 25 99
You may notice that while accessing a cell, we have specified the index and column as .at[0, 'Col1']
, where the first argument is the index and the second argument is the column.
If you omit the column and specify only the index, all values at that index will be modified.
Use Dataframe.set_value()
the method to set values for specific cells in a pandas DataFrame
Another option is Dataframe.set_value()
the method. This is very similar to the previous method, accessing values one at a time, but the syntax is slightly different.
import pandas as pd
sample_df = pd.DataFrame(
[[10, 20, 30], [11, 21, 31], [15, 25, 35]],
index=[0, 1, 2],
columns=["Col1", "Col2", "Col3"],
)
print "\nOriginal DataFrame"
print (pd.DataFrame(sample_df))
sample_df.set_value(0, "Col1", 99)
sample_df.set_value(1, "Col2", 99)
sample_df.set_value(2, "Col3", 99)
print "\nModified DataFrame"
print (pd.DataFrame(sample_df))
Output:
Original DataFrame
Col1 Col2 Col3
0 10 20 30
1 11 21 31
2 15 25 35
Modified DataFrame
Col1 Col2 Col3
0 99 20 30
1 11 99 31
2 15 25 99
Use Dataframe.loc
the method to set values for specific cells in a pandas DataFrame
Another possible way to set specific cells, with slightly different syntax, is dataframe.loc
the method.
import pandas as pd
sample_df = pd.DataFrame(
[[10, 20, 30], [11, 21, 31], [15, 25, 35]],
index=[0, 1, 2],
columns=["Col1", "Col2", "Col3"],
)
print "\nOriginal DataFrame"
print (pd.DataFrame(sample_df))
sample_df.loc[0, "Col3"] = 99
sample_df.loc[1, "Col2"] = 99
sample_df.loc[2, "Col1"] = 99
print "\nModified DataFrame"
print (pd.DataFrame(sample_df))
Output:
Original DataFrame
Col1 Col2 Col3
0 10 20 30
1 11 21 31
2 15 25 35
Modified DataFrame
Col1 Col2 Col3
0 10 20 99
1 11 99 31
2 99 25 35
All the above methods in this article DataFrame
are convenient ways to modify or set a specific cell in Pandas, with minor differences in syntax and conventions.
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
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
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
Install and use Elasticsearch and MongoDB on Windows and Ubuntu
Publish Date:2025/04/29 Views:70 Category:MongoDB
-
This article is a step-by-step installation and configuration guide for Elasticsearch on Windows and Ubuntu 20.04. It also demonstrates using Elasticsearch with MongoDB on both operating systems. What is Elasticsearch Elasticsearch is a NoS
Fuzzy search in MongoDB
Publish Date:2025/04/29 Views:58 Category:MongoDB
-
Today, we will discuss fuzzy search and how to do it with MongoDB. We will start by using $regex the operator and $text the query. In addition, we will learn to Fuse.js perform fuzzy search on documents using a JavaScript library called . W
Update multiple documents in MongoDB
Publish Date:2025/04/29 Views:73 Category:MongoDB
-
This article will discuss how to efficiently update multiple documents in MongoDB. updateMany() Methods in MongoDB Using the update method in MongoDB db.collection.updateMany() , you can update multiple documents in a collection. This metho
Search in a specified array in MongoDB
Publish Date:2025/04/29 Views:152 Category:MongoDB
-
$in Operator searches within an array that you provide. For example, if you are given an array and want to search for one or more matching elements in a MongoDB collection, you can use $in . Use $in the operator to search for a specified ar
Unique Index in MongoDB
Publish Date:2025/04/28 Views:130 Category:MongoDB
-
In this article, you'll learn about unique indexes, including what they are and how to create them in MongoDB. Additionally, the process of making a user's email unique in MongoDB is briefly described. The contents of this article are as fo
Creating Indexes in MongoDB
Publish Date:2025/04/28 Views:179 Category:MongoDB
-
Indexes help resolve queries efficiently. Without indexes, MongoDB must iterate through every document in the collection to find the documents that match the query. It will waste time and require MongoDB to handle such information. Therefor
Sparse Indexes in MongoDB
Publish Date:2025/04/28 Views:97 Category:MongoDB
-
In MongoDB, it is not mandatory for all documents to contain the same fields. Now, if you want to get all the documents in a MongoDB collection that contain only a specific field, then you may need to specify a common field as an index. Thi