Oliver Spohngellert
Oliver Spohngellert

Reputation: 123

Filtering a pandas dataframe properly

I am trying to filter a pandas dataframe using a function and am running into SettingWithCopyWarning warning. I was wondering if there was a better way of doing this. Below is a general outline of my code:

def cleanData(data):
    out = data.query("data.x < 100")
    out.z = out.z == "Z"
    return out
data = cleanData(data)

I would like to be able to keep it in this function form, as I want to run the function on both my train and test data. Thanks :)

Upvotes: 1

Views: 104

Answers (2)

Ken Wei
Ken Wei

Reputation: 3130

Just use .copy():

def cleanData(data):
    out = data.query("data.x < 100").copy()
    out.z = out.z == "Z"
    return out
data = cleanData(data)

Upvotes: 3

jezrael
jezrael

Reputation: 862781

You can use copy:

out = data.query("data.x < 100").copy()

If you modify values in out later you will find that the modifications do not propagate back to the original data (data), and that Pandas does warning.

Upvotes: 1

Related Questions