尝试删除列时Pandas警告

use*_*827 8 python pandas

    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   
Run Code Online (Sandbox Code Playgroud)

在上面的数据框中,我尝试使用foll删除名为"Item"的列.命令:

vals_bel_lux.drop('Item', axis=1, inplace=True)
Run Code Online (Sandbox Code Playgroud)

然而,这给了我一个人.警告:

    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
Run Code Online (Sandbox Code Playgroud)

我该如何修复此警告?

Ana*_*mar 12

很可能你收到了vals_bel_lux切片,在这种情况下问题就出现了,因为你试图做inplacedrop(通过将inplace=True参数传递给drop方法).

如果您想要的是删除了列的新数据帧,则可以删除该参数并接受DataFrame返回的新参数.示例 -

vals_bel_lux_new = vals_bel_lux.drop('Item', axis=1)
Run Code Online (Sandbox Code Playgroud)