-2

I have a pandas dataframe in python and I'm trying to modify a specific value in a particular row. I found a solution to this problem Set value for particular cell in pandas DataFrame using index, but it is still generating the SettingWithCopy error.

The name of the data frame is internal_df and it has columns 'price', 'visits', and 'orders'. Specifically, I want to add the number of orders and visits to a lower price point if we don't have a sufficient number of visits (100 in this example). Note that below the variable 'price' is a float, and the data types for 'price' within the internal_df data frame is float, while price and orders are ints.

if int(internal_df[internal_df['price']==price]['visits']) < 100:
 for index, row in internal_df.iterrows():
   if float(row['price']) > price:
      internal_df.ix[internal_df['price'] == price, 'visits'] = internal_df.ix[internal_df['price'] == price, 'visits'] + row['visits']
      internal_df.ix[internal_df['price'] == price, 'orders'] = internal_df.ix[internal_df['price'] == price, 'orders'] + row['orders']

Here is a sample of the data

    price   visits  sales
0   1399.99 2   0
1   169.99  2   0
2   99.99   1   0
3   99.99   1   0
4   139.99  1   0
5   319.99  1   0
6   198.99  1   0
7   119.99  1   0
8   39.99   1   0
9   259.98  1   0

Does anyone have any suggestions, or should I just ignore the error?

Brad

Brad Davis
  • 919
  • 3
  • 15
  • 34

1 Answers1

1

Note that .ix is deprecated because it indexes by position or by label, depending on the data type of the index. Use .loc or .iloc instead.

This SettingWithCopyWarning might originate from a "get" operation several lines of code above what you've provided. A quick fix might be to find where internal_df is first assigned, and to add .copy() to the end of the assignment statement. For example, if you have internal_df = df[df['colname'] <= value], change that to internal_df = df[df['colname'] <= value].copy() and hopefully that resolves the error.

Also, I think you can do what you're trying to do without a for loop, which would be faster and more readable!

Peter Leimbigler
  • 7,579
  • 1
  • 13
  • 24