• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / pandas set_value – Using at() Function to Set a Value in DataFrame

pandas set_value – Using at() Function to Set a Value in DataFrame

January 21, 2022 Leave a Comment

To set a value in a pandas DataFrame, the easiest way is to use the pandas at() function.

df.at[row,column] = value

The pandas set_value() method was deprecated in version 0.21.


When working with data, the ability to update fields on the fly can be very useful. We can use the pandas at() function to set values in a DataFrame or a Series.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Age': [43,23,71,49,52,37], 
      'Test_Score':[90,87,92,96,84,79]})

print(df)
# Output: 
   Age  Test_Score
0   43          90
1   23          87
2   71          92
3   49          96
4   52          84
5   37          79

To set a value in this DataFrame, we can use the pandas at() function. Let’s say we want to update the 4th row’s Age from 49 to 51. We can do this easily in the following Python code.

df.at[3,"Age"] = 51

print(df)
# Output: 
   Age  Test_Score
0   43          90
1   23          87
2   71          92
3   51          96
4   52          84
5   37          79

If you want to set a value based on integer positions of the columns, you can use the pandas iat() function.

df.iat[3,0] = 51

print(df)
# Output: 
   Age  Test_Score
0   43          90
1   23          87
2   71          92
3   51          96
4   52          84
5   37          79

Setting a New Value in a Series in pandas

We can also set values in Series using the pandas at() function.

Let’s say we have the following Series.

ser = pd.Series(df["Age"])

print(ser)
0    43
1    23
2    71
3    51
4    52
5    37
Name: Age, dtype: int64

To set a value in a Series, pass the index you want to change to at() and then set it to the value you want.

ser.at[1] = 25
print(ser)

0    43
1    25
2    71
3    51
4    52
5    37
Name: Age, dtype: int64

Hopefully this article has helped you understand how to set values in pandas DataFrames and Series.

Other Articles You'll Also Like:

  • 1.  Get First Key and Value in Dictionary with Python
  • 2.  Using Python to Sort Two Lists Together
  • 3.  Get Current Datetime in Python
  • 4.  Check if Variable is Float in Python
  • 5.  How Clear a Set and Remove All Items in Python
  • 6.  Python Negative Infinity – How to Use Negative Infinity in Python
  • 7.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()
  • 8.  Add Seconds to Datetime Variable Using Python timedelta() Function
  • 9.  How to Check if a Dictionary is Empty in Python
  • 10.  nunique pandas – Get Number of Unique Values in DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy