Here is an example that simulates a warning situation:
import pandas as pd
Import NUMPY AS NP
df = pd.dataframe ({"col1": [1, 2, 3], "col2": ["a", "b", "a"]})
... # Do Something
df1 = df [df.col2 == "a"]
DF1 ["new_column"], df1 ["Case"] = np.nan, ""
This completely harmless code does what you need, and this is confirmed by the conclusion conclusion:
df1
Out [16]:
COL1 COL2 New_Column Case
0 1 a nan
2 3 a nan
df.
Out [17]:
col1 col2
0 1 A.
1 2 B.
2 3 A.
However, the fun warning is departed, which scares its incomprehensibility:
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
DF1 ["new_column"], df1 ["Case"] = np.nan, ""
that recommends doing something different.
Two questions:
- for some reason I add a column – it’s bad, judging by the prevention
- How should I add a column with a given value
Answer 1, Authority 100%
Despite the fact that the error indicates this line:
df1 ["new_column"], df1 ["case"] = np.nan, ""
root problems you have here in this line:
df1 = df [df.col2 == "a"]
Here you create link on the DF Data Cut. What you warn you about. You are required to explicitly specify whether you are working with a reference (view) or copy (COPY). In your case, one solution may be an explicit creation of a copy of DF:
df1 = df [df.col2 == "a"]. Copy ()