Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set value for particular cell in pandas DataFrame with iloc

Tags:

python

pandas

I have a question similar to this and this. The difference is that I have to select row by position, as I do not know the index.

I want to do something like df.iloc[0, 'COL_NAME'] = x, but iloc does not allow this kind of access. If I do df.iloc[0]['COL_NAME'] = x the warning about chained indexing appears.

like image 934
luna1999 Avatar asked Jul 22 '15 16:07

luna1999


People also ask

How do I change the value of a specific cell in pandas DataFrame?

You can set cell value of pandas dataframe using df.at[row_label, column_label] = 'Cell Value'. It is the fastest method to set the value of the cell of the pandas dataframe.

How do I change values in ILOC?

Using iloc() method to update the value of a row With the Python iloc() method, it is possible to change or update the value of a row/column by providing the index values of the same. In this example, we have updated the value of the rows 0, 1, 3 and 6 with respect to the first column i.e. 'Num' to 100.

How do I select a specific cell in a DataFrame?

You can get cell value by column name by using the values[] attribute. First, select the specific column by using its name using df['Product_Name'] and get the value of a specific cell using values[0] as shown below. What is this? This is how you can get the cell value of a dataframe by using the column name.


2 Answers

For mixed position and index, use .ix. BUT you need to make sure that your index is not of integer, otherwise it will cause confusions.

df.ix[0, 'COL_NAME'] = x 

Update:

Alternatively, try

df.iloc[0, df.columns.get_loc('COL_NAME')] = x 

Example:

import pandas as pd import numpy as np  # your data # ======================== np.random.seed(0) df = pd.DataFrame(np.random.randn(10, 2), columns=['col1', 'col2'], index=np.random.randint(1,100,10)).sort_index()  print(df)         col1    col2 10  1.7641  0.4002 24  0.1440  1.4543 29  0.3131 -0.8541 32  0.9501 -0.1514 33  1.8676 -0.9773 36  0.7610  0.1217 56  1.4941 -0.2052 58  0.9787  2.2409 75 -0.1032  0.4106 76  0.4439  0.3337  # .iloc with get_loc # =================================== df.iloc[0, df.columns.get_loc('col2')] = 100  df        col1      col2 10  1.7641  100.0000 24  0.1440    1.4543 29  0.3131   -0.8541 32  0.9501   -0.1514 33  1.8676   -0.9773 36  0.7610    0.1217 56  1.4941   -0.2052 58  0.9787    2.2409 75 -0.1032    0.4106 76  0.4439    0.3337 
like image 66
Jianxun Li Avatar answered Oct 14 '22 20:10

Jianxun Li


One thing I would add here is that the at function on a dataframe is much faster particularly if you are doing a lot of assignments of individual (not slice) values.

df.at[index, 'col_name'] = x 

In my experience I have gotten a 20x speedup. Here is a write up that is Spanish but still gives an impression of what's going on.

like image 41
ford prefect Avatar answered Oct 14 '22 20:10

ford prefect