2

I am trying to assign values to rows where a condition is verified (True/False).

for i in range(0,3):

new_dataset=df[str(i)][df[str(i)]["Current Amount"] != "3m"]
  for i in range(0,3):
    df[i]['Value'] = np.where(df[i]['Amount']== True, 100, 50)

where i can span from 0 to 3. Value is the new column that I would like to create; Amount is a column already existing in the original dataframe. In the first part, I create new dataframes filtering rows having current amounts equal to 3 million.

However I got the following error:

   /anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
    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
      after removing the cwd from sys.path.

I have tried to follow the steps suggested in this post: How to deal with SettingWithCopyWarning in Pandas?, but it seems that it is still continuing to be not clear to me how to fix the issue.

Could you please help me to fix the issue? I would really appreciate it.

still_learning
  • 776
  • 9
  • 32

1 Answers1

2

Why is not used solution without [i] and compare by True if column is boolean?

df['Value'] = np.where(df['Amount'], 100, 50)

EDIT: Here is necessary DataFrame.copy:

new_dataset=df[str(i)][df[str(i)]["Current Amount"] != "3m"].copy()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Because I am considering many dataframes and I am trying to do this using a for loop statement, instead of doing it one by one. – still_learning May 11 '20 at 12:10
  • 1
    @still_learning - OK, is possible get 3-5 rows of your code before error? – jezrael May 11 '20 at 12:11
  • I updated the question. Thank you for your help. Let me know if you need more code. The columns are boolean in all the dataframes (I used `astype('bool')` to convert them at a certain point) – still_learning May 11 '20 at 12:19
  • could you please explain me why I need to use .copy()? Does it create a list of dataframes or multiple dataframes too? – still_learning May 11 '20 at 12:22
  • 1
    @still_learning - If you modify values in `new_dataset` later you will find that the modifications do not propagate back to the original data (`df`), and that Pandas does warning. – jezrael May 11 '20 at 12:24