How can I set a specific value for a cell in a Pandas DataFrame?

How can I set a specific value for a cell in a Pandas DataFrame?

Setting a specific value for a cell in a Pandas DataFrame involves accessing the desired cell using its index or column label and assigning the desired value to it. This can be achieved by using the .loc or .iloc methods, depending on whether the index or position of the cell is known. Alternatively, the .at or .iat methods can be used for faster performance when setting a single value. It is important to note that any changes made to a Pandas DataFrame are reflected in the original DataFrame, unless explicitly stated otherwise.

Set Value for a Specific Cell in Pandas DataFrame


You can use the following basic syntax to set the value for a specific cell in a pandas DataFrame:

#set value at row index 0 and column 'col_name' to be 99
df.at[0, 'col_name'] = 99

The following examples show how to use this syntax in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

	points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10
3	14	9	6
4	19	12	6
5	23	9	5
6	25	9	9
7	29	4	12

Example 1: Set Value of One Cell in Pandas 

The following code shows how to set the value in the 3rd index position of the ‘points’ column to 99:

#set value in 3rd index position and 'points' column to be 99
df.at[3, 'points'] = 99

#view updated DataFrame
df

        points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10
3	99	9	6
4	19	12	6
5	23	9	5
6	25	9	9
7	29	4	12

Notice that the value in the 3rd index position of the ‘points’ column was changed and all other values in the DataFrame remained the same.

Example 2: Set Value of Multiple Cells in Pandas

The following code shows how to set the value of multiple cells in a range simultaneously:

#set values in index positions 0 to 3 in 'points' column to be 99 
df.at[0:3, 'points'] = 99

#view updated DataFramedf

	points	assists	rebounds
0	99	5	11
1	99	7	8
2	99	7	10
3	99	9	6
4	19	12	6
5	23	9	5
6	25	9	9
7	29	4	12

Example 3: Set Values Conditionally in Pandas

The following code shows how to set the values in the ‘rebounds’ column to be 99 only if the value in the points column is greater than 20:

#set values in 'rebounds' column to be 99 if value in points column is greater than 20
df.loc[df['points']>20, ['rebounds']] = 99

#view updated DataFrame
df	points	assists	rebounds
0	25	5	99
1	12	7	8
2	15	7	10
3	14	9	6
4	19	12	6
5	23	9	99
6	25	9	99
7	29	4	99

Notice that each value in the rebounds column was changed to 99 if the value in the points column was greater than 20.

All other values remained the same.

Cite this article

stats writer (2024). How can I set a specific value for a cell in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-set-a-specific-value-for-a-cell-in-a-pandas-dataframe/

stats writer. "How can I set a specific value for a cell in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 6 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-set-a-specific-value-for-a-cell-in-a-pandas-dataframe/.

stats writer. "How can I set a specific value for a cell in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-set-a-specific-value-for-a-cell-in-a-pandas-dataframe/.

stats writer (2024) 'How can I set a specific value for a cell in a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-set-a-specific-value-for-a-cell-in-a-pandas-dataframe/.

[1] stats writer, "How can I set a specific value for a cell in a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I set a specific value for a cell in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top