0

So, I have a dataframe with several columns, but the ones that really matter are called A and B. If column A does not contains value X or If column B does not contain value Y, that row must be dropped.

I tried using this function:

def removeRows(df, value):
  
  df.drop(df[ (df['A'] != value) | (df['B'] != value)].index, inplace = True)
  return df



But i got this error: 

A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
Marcos Dias
  • 420
  • 3
  • 9
  • You can use `~` to invert your boolean mask and assign it that way as in `df=df[~df['A'] !=...` – G. Anderson Mar 09 '21 at 18:47
  • That doesn't solve my problem. I need a way to drop a row if column A or B (or both) contains the value i'm looking for – Marcos Dias Mar 09 '21 at 19:05
  • An inverted boolean mask absolutely should work to do exactly that, but it would help if you include a [mcve] with sample input and expected output so that we can reproduce your issue. See [how to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Mar 09 '21 at 21:20

1 Answers1

0

I suggest that you add an example of how you used removeRows as looks like it should not raise this warning.
This warning is raised on when calling removeRows with part of the data frame.

For example:

import pandas as pd
df = pd.DataFrame({
    "A": [1,2,3,4,5],
    "B": [1,7,8,9,10],
    "C": [1,2,3,4,5]
})
df = removeRows(df.loc[df['C'] > 1], 3)

Will raise this warning. Try changing your code to

def removeRows(df, value):
    df = df.loc[(df['A'] != value) | (df['B'] != value)]
    return df

mannh
  • 21
  • 3