How to change update cell value in Python Pandas dataframe

+1 vote

I have the following dataframe

  Name Age
0 Mike 23
1 Eric 25
2 Donna 23
3 Will 23

And I want to change the age of Mike. How can I do this?

Apr 8, 2019 in Python by Raj
146,465 views

3 answers to this question.

+1 vote

You can use the at() method to do this

df.at[0,’Age']= 20


Hope it helps!!

If you need to know more about Python, It's recommended to join Python course today.

Thanks!

answered Apr 8, 2019 by Kunal
when using df.at[row_num, "Col B"] = "yes":

the at() method added a new column in my data frame instead of modifying the
Col B of my dataframe.

Was this the case for anyone else?
Hi @ken, Can you please share your data frame. The syntax of df.at() is correct but probably you aren't using it in accordance with your data frame.
Hey :) I was having the same issue, and using df.iat[] instead of df.at[] worked for me

"df.at[0,’Age']= 20" is not the correction solution

Hi, @There,

Even I have faced the same issue but the above-given solution has worked for me. Could you please share your workaround so that we can analyze why the solution isn't working!
0 votes

Hi,

You can use at() method to update your dataset. I have attached one example for your reference.

import pandas as pd
df1=pd.read_csv('my.csv')
Place State
0 Kolkata WestBengal
1 Delhi Delhi
2 Bangalore Karnataka
3 Kolkata WestBengal
4 Delhi Delhi
df1.at[0,'Place']= 'Mumbai'
Place State
0 Mumbai WestBengal
1 Delhi Delhi
2 Bangalore Karnataka
3 Kolkata WestBengal
4 Delhi Delhi

I hope this will help you.

answered Jun 26, 2020 by MD
• 95,440 points
0 votes

Use iloc:

df.iloc[2,3] = 32

print(df)
#   A1  B1  C1  D1
#0   0   0   0   0
#1   0   0   0   0
#2   0   0   0  32
#3   0   0   0   0

Or if you want to modify by index and column name, use loc:

df.loc[2, 'D1'] = 32
answered Dec 9, 2020 by anonymous
• 82,880 points

Related Questions In Python

0 votes
1 answer

How to set value for particular cell in pandas DataFrame using index?

Why df.xs('C')['x']=10 does not work: df.xs('C') by default, returns a new ...READ MORE

answered Jan 5, 2021 in Python by Gitika
• 65,910 points
3,171 views
0 votes
1 answer

How to get value in Pandas dataframe using index?

You can use the iloc method to do ...READ MORE

answered Apr 8, 2019 in Python by Rohit
7,517 views
0 votes
1 answer

How to check if any value is NaN in a Pandas DataFrame?

Hello @kartik, If you need to know how ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
3,904 views
0 votes
1 answer

How to check if a value exists in pandas dataframe index?

Hello @kartik, Basically instead of raising exception I ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
6,107 views
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by MD 1,638 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is "size" includes NaN values, ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by Gitika 2,484 views
0 votes
2 answers

Replacing a row in pandas data.frame

key error. I love python READ MORE

answered Feb 18, 2019 in Data Analytics by anonymous
12,971 views
0 votes
1 answer

Converting a pandas data-frame to a dictionary

Emp_dict=Employee.to_dict('records') You can directly use the 'to_dict()' function ...READ MORE

answered May 23, 2018 in Data Analytics by Bharani
• 4,660 points
4,323 views
0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,560 points
34,063 views
0 votes
1 answer

How to find if a value exists in Pandas dataframe?

Try this:​ for name in df['Name']: ...READ MORE

answered Apr 8, 2019 in Python by Tina
12,271 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP