Skip to content Skip to sidebar Skip to footer

Python View Vs Copy Error Wants Me To Use .loc In Script Only

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 again in the command line: A

Solution 1:

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'

Post a Comment for "Python View Vs Copy Error Wants Me To Use .loc In Script Only"