0

i want to change 10 to 5 in index 0 and column name 'x'. my code is:

mydata = {'x' : [10, 50, 18, 32, 47, 20], 'y' : ['12', '11', 'N/A', '13', '15', 'N/A']}
df1 = pd.DataFrame(mydata)
df1['x'][df1['x'] == 10] = 5

and i got:

C:\Users\T\AppData\Local\Temp\ipykernel_2260\1154778325.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df1['x'][df1['x'] == 10] = 5

its work, but with this warning

2 Answers2

1

This is where you need to use the loc function:

df1.loc[df1['x'] == 10, 'x'] = 5

Here's why this is an issue. Think about how Python implements your statement:

(df1['x'])[df1['x'] == 10] = 5

That first expression extracts a column from your dataframe and returns it as a separate Series object, which is called a "slice". You can change things in that slice, but it's a separate object -- the changes won't touch the original dataframe.

The loc function handles both indexes in one shot.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

Might suggest using .replace:

import pandas as pd
mydata = {
    'x': [10, 50, 18, 32, 47, 20],
    'y': ['12', '11', 'N/A', '13', '15', 'N/A']
}
df1 = pd.DataFrame(mydata)
df1['x'].replace(10, 5, inplace=True)

Output:

    x    y
0   5   12
1  50   11
2  18  N/A
3  32   13
4  47   15
5  20  N/A

However, that would not apply if you need more "surgical" approach, e.g. replacing only the first or the n-th occurrence.

alphamu
  • 383
  • 1
  • 3
  • 9