Pandas warning when deleting a column

Item Y1961 Y1962 Y1963 Y1964 Y1965 Y1966 Y1967 Y1968 \ 8 Wheat 212139 212221 201443 217656 229353 231643 216676 220347 Y1969 ... Y2004 Y2005 Y2006 Y2007 Y2008 Y2009 Y2010 Y2011 \ 8 215759 ... 0 0 0 0 0 0 0 0 

In the above piece of data, I'm trying to delete a column named "Item" using foll. Command:

 vals_bel_lux.drop('Item', axis=1, inplace=True) 

However, this gives me the following. a warning:

  C:\Anaconda64\lib\site-packages\pandas\core\generic.py:2602: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 

How can I fix this warning?

+5
source share
1 answer

Most likely you got vals_bel_lux through slicing, and in this case the problem arises because you are trying to do inplace drop (passing the inplace=True argument to the drop method).

If all you need is a new framework with a deleted column, you can remove this argument and accept a new DataFrame to be returned. Example -

 vals_bel_lux_new = vals_bel_lux.drop('Item', axis=1) 
+4
source

Source: https://habr.com/ru/post/1232158/


All Articles