0

I have a small DataFrame that I would like to filter by certain values. The result of this should be saved in a new column with a new value. Currently I do this (see below), but I always get the SettingWithCopyWarning message. But the result of the task is correct.

how can I prepare the code so that no message is triggered? And I don't really want to suppress the message.

DataFrame:

        State       Temp
0       1           22.0
98      6           199.0
242     1           20.0
1011    6           200.0
1363    1           9.0

Code:

df_phase.loc[df_phase["State"] == 1, 'State_New'] = 1
df_phase.loc[df_phase["State"] == 6, 'State_New'] = 7
df_phase.loc[df_phase["State"] == 7, 'State_New'] = 6

Resulting Warning:

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
  self._setitem_single_column(loc, value, pi)
Bodydrop
  • 21
  • 4
  • This code shouldn't give a `SettingWithCopyWarning`, the issue must be prior to that (how did you generate `df_phase`?) – mozway Mar 08 '22 at 08:42

0 Answers0