0

I'm new to pandas, I just want to drop some rows in a DataFrame inplace, but there are warnings:

/opt/conda/lib/python3.7/site-packages/pandas/core/frame.py:4315: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

the function that gives errors:

def single_drop(df: pd.DataFrame):
    """Drop rows with '-99' entries."""

    reduced_df = df
    for i in range(len(reduced_df)):
        if reduced_df.loc[i].isin([-99]).any():
            reduced_df.drop([i], inplace=True)

I don't think I have used chained assignment, or I have?

and I try to repalce the code to:

reduced_df = reduced_df.drop([i])

the error is gone.

So how to use inplace correctly?

  • see https://stackoverflow.com/questions/43893457/understanding-inplace-true – Anurag Dabas Aug 04 '21 at 07:09
  • It's actually recommended to avoid using``inplace`` and just reassign ``df = df.whateveryourdoing`` because it's considered harmful and will be deprecated in the near future, see- https://stackoverflow.com/a/60020384/13138364 – Tomer Poliakov Aug 04 '21 at 07:22

0 Answers0