Python view vs copy error wants me to use .loc in script only

后端 未结 1 1626
萌比男神i
萌比男神i 2021-01-27 07:22

I\'m running a long script which has a dataframe df. as the script runs, building up and modifying df column by column I get this error over and over a

1条回答
  •  执笔经年
    2021-01-27 07:49

    As I mentioned in my comment, the likely issue is that somewhere upstream in your code, you assigned a slice of some other pd.DataFrame to df. This is a common cause of confusion and is also explained under why-does-assignment-fail-when-using-chained-indexing in the link that the Warning mentions.

    A minimal example:

    data = pd.DataFrame({'a':range(7), 'b':list('abcccdb')})
    df = data[data.a % 2 == 0]  #making a subselection of the DataFrame  
    df['b'] = 'b'
    

    /home/user/miniconda3/envs/myenv/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

    See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy """Entry point for launching an IPython kernel.

    Notice that this section:

    df = data[data.a % 2 == 0]  #making a subselection of the DataFrame  
    df['b'] = 'b'
    

    could just as well be rewritten this way:

    data[data.a % 2 == 0]['b'] = 'b'  #obvious chained indexing  
    df = data[data.a % 2 == 0]
    

    The correct way of writing this bit is the following way:

    data = pd.DataFrame({'a':range(7), 'b':list('abcccdb')})
    df = data.loc[data.a % 2 == 0].copy()  #making a copy of the subselection   
    df.loc[:,'b'] = 'b'
    

    0 讨论(0)
提交回复
热议问题