Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

I have used the below code for creating the Data Frame using Pandas.

df = DataFrame(index=['A','B','C'], columns=['x','y'])

And got output:

    x    y

A  NaN  NaN

B  NaN  NaN

C  NaN  NaN

I am trying to assign a value to a particular cell in the data frame. For example value of c in the c that is 10, I need to extract and change the value.

    x    y

A  NaN  NaN

B  NaN  NaN

C  10   NaN

To do that I am using this code:

df.xs('C')['x'] = 9

When I use this code there is no change in the content of that particular cell.

Can anyone suggest me a solution?

1 Answer

0 votes
by (36.8k points)

Using this code as you have used

df.xs('C')['x'] = 9

will not work because that data frame will not get duplicated. You have a different method to do it. That is '.iat', this helps to give a new value for the cell. 

Go through the code:

df.at['c','x']=9

OR

df.iat[2,1]=9

Using the above code you can change the cell value in the data frame.

Hear df means data frame, '.iat' locates the cell by an integer and '.at' is locating the cell with the name of the cell.

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...