0
df.drop(['column_name'],axis=1,inplace=True)

gives

"Warning (from warnings module):
  File "/home/sourav/.local/lib/python3.5/site-packages/pandas/core/frame.py", line 3697
    errors=errors)
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy"
meW
  • 3,832
  • 7
  • 27
Datahat
  • 3
  • 3

3 Answers3

2

I believe your df is a (not-properly-done) copy of another pandas.DataFrame.

Following reproduces the SettingWithCopyWarning.

import pandas as pd

raw = pd.DataFrame({"a": [1,2], "b": [2,3], "c": [3,4]})
df = raw[["a", "b"]]
df.drop(["a"], 1, inplace = True)

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy errors=errors)

When you make another pandas.DataFrame from an existing one, never do direct assignment like df = raw[["a", "b"]]. Instead, use pandas.DataFrame.copy().

raw = pd.DataFrame({"a": [1,2], "b": [2,3], "c": [3,4]})
df = raw[["a", "b"]].copy()
df.drop(["a"], 1, inplace = True)

Now the warning disappears, as df is a new object created, and thus according to the official document:

Modifications to the data or indices of the copy will not be reflected in the original object

Chris
  • 29,127
  • 3
  • 28
  • 51
0

This is a very common warning when working with pandas library, but according to dataquest, I can give you some of the most important tips in short.

  • The first thing to understand is that SettingWithCopyWarning is a warning, and not an error.
  • It can be tempting to ignore the warning if your code still works as expected. This is bad practice and SettingWithCopyWarning should never be ignored. Take some time to understand why you are getting the warning before taking action.

I refer you to the website itself for more detailed information.

Meysam
  • 596
  • 6
  • 12
0

The drop usually doesn't create a SettingWithCopyWarning.
But there's a chance that the dataframe you're getting is generated dynamically through slicing.

In that case try this:

data_new = data.drop('columnname', axis= 1)
Yogesh Patel
  • 818
  • 2
  • 12
  • 26