21

This is one of the lines in my code where I get the SettingWithCopyWarning:

value1['Total Population']=value1['Total Population'].replace(to_replace='*', value=4)

Which I then changed to :

row_index= value1['Total Population']=='*'
value1.loc[row_index,'Total Population'] = 4

This still gives the same warning. How do I get rid of it?

Also, I get the same warning for a convert_objects(convert_numeric=True) function that I've used, is there any way to avoid that.

 value1['Total Population'] = value1['Total Population'].astype(str).convert_objects(convert_numeric=True)

This is the warning message that I get:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 
Henry Ecker
  • 31,792
  • 14
  • 29
  • 50
Pragnya Srinivasan
  • 383
  • 1
  • 4
  • 12
  • Because your are updating the same set of values in that dataframe at the same time, you can try to use a temp variable to hold the result and apply back to the column, see if this helps ridding of the warning message. – Anzel Sep 14 '15 at 20:56
  • 1
    what version of python & pandas are you using? – PabTorre Sep 14 '15 at 21:11

8 Answers8

26

If you use .loc[row,column] and still get the same error, it's probably because of copying another data frame. You have to use .copy().

This is a step by step error reproduction:

import pandas as pd

d = {'col1': [1, 2, 3, 4], 'col2': [3, 4, 5, 6]}
df = pd.DataFrame(data=d)
df
#   col1    col2
#0  1   3
#1  2   4
#2  3   5
#3  4   6

Creating a new column and updating its value:

df['new_column'] = None
df.loc[0, 'new_column'] = 100
df
#   col1    col2    new_column
#0  1   3   100
#1  2   4   None
#2  3   5   None
#3  4   6   None

No error I receive. However, let's create another data frame given the previous one:

new_df = df.loc[df.col1>2]
new_df
#col1   col2    new_column
#2  3   5   None
#3  4   6   None

Now, using .loc, I will try to replace some values in the same manner:

new_df.loc[2, 'new_column'] = 100

However, I got this hateful warning again:

A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

SOLUTION

use .copy() while creating the new data frame will solve the warning:

new_df_copy = df.loc[df.col1>2].copy()
new_df_copy.loc[2, 'new_column'] = 100

Now, you won't receive any warnings!

If your data frame is created using a filter on top of another data frame, always use .copy().

Hadij
  • 2,380
  • 5
  • 22
  • 38
7

Have you tried setting directly?:

value1.loc[value1['Total Population'] == '*', 'Total Population'] = 4
Alexander
  • 96,739
  • 27
  • 183
  • 184
2

I came here because I wanted to conditionally set the value of a new column based on the value in another column.

What worked for me was numpy.where:

import numpy as np
import pandas as pd
...

df['Size'] = np.where((df.value > 10), "Greater than 10", df.value)

From numpy docs, this is equivelant to:

[xv if c else yv
 for c, xv, yv in zip(condition, x, y)]

Which is a pretty nice use of zip...

kztd
  • 2,533
  • 18
  • 14
1

I have no idea how bad the data storage/memory implications are with this but it fixes it every time for your average dataframe:

def addCrazyColFunc(df):
    dfNew = df.copy()
    dfNew['newCol'] = 'crazy'
    return dfNew

Just like the message says... make a copy and you're good to go. Please if someone can fix the above without the copy, please comment. All the above loc stuff doesn't work for this case.

blissweb
  • 1,829
  • 2
  • 15
  • 28
0

Got the solution:

I created a new DataFrame and stored the value of only the columns that I needed to work on, it gives me no errors now!

Strange, but worked.

Pragnya Srinivasan
  • 383
  • 1
  • 4
  • 12
0

Specifying it is a copy worked for me. I just added .copy() at the end of the statement

value1['Total Population'] = value1['Total Population'].replace(to_replace='*', value=4).copy()
Douglas Ferreira
  • 659
  • 2
  • 5
  • 22
Rodrigo
  • 1
  • 1
0

This should fix your problem :

value1[:, 'Total Population'] = value1[:, 'Total Population'].replace(to_replace='*', value=4)
-1

I was able to avoid the same warning message with syntax like this:

value1.loc[:, 'Total Population'].replace('*', 4)

Note that the dataframe doesn't need to be re-assigned to itself, i.e. value1['Total Population']=value1['Total Population']...

abstrakkt
  • 56
  • 6