Try using .LOC --- Message

24 views
Skip to first unread message

Bibeesh Y S

unread,
Jun 8, 2020, 10:41:02 AM6/8/20
to spyder
Hi,

I trying to create a new column names "BUY_SELL" in a dataframe "R2"
Syntax : R2["BUY_SELL"]="SELL"

No error while executing the above line alone but when executing the hole python script below message is appearing


E:\BIBEESH\PYTHON\NSE\Basan_Version_1.ipy:295: 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

Even I tried iterate using for loop, still the msg persists.

is this a problem or ignorable?

Regards
Bibeesh

Jim Lareau

unread,
Jun 9, 2020, 12:36:24 AM6/9/20
to spyder
Yet again, a simple Google search:

pandas dataframe add column

yields as one result, the url:

Adding new column to existing DataFrame in Pandas - GeeksforGeeks
https://www.geeksforgeeks.org/adding-new-column-to-existing-dataframe-in-pandas/

Which answers your question.

Regards,

Jim

Bibeesh Y S

unread,
Jun 9, 2020, 11:38:32 AM6/9/20
to spyd...@googlegroups.com
Thanks for replying, I'm able to add new columns with LOC or without LOC. 
My question is why the below message coming while executing the syntax

E:\BIBEESH\PYTHON\NSE\Basan_Version_1.ipy:295: 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
--
You received this message because you are subscribed to the Google Groups "spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spyderlib+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/spyderlib/0142af4e-b02c-4660-b2a8-4c507bcebeabo%40googlegroups.com.

bcolsen

unread,
Jun 11, 2020, 12:04:47 AM6/11/20
to spyder
This happens because R2 is a subsection of another dataframe(R1 I guess) usually made with a slice or a query. Pandas can't guaranty whether that change will be applied to the dataframe because it doesn't know whether it is a  view or a copy of the original dataframe. If you don't care if the change was made to the original as well you can make a copy or you can check the change occurred in the original and ignore the warning, but this could lead to difficult to detect bugs that's why they put in the warning.

please see the link in the warning for more information.


On Tuesday, June 9, 2020 at 9:38:32 AM UTC-6, Bibeesh Y S wrote:
Thanks for replying, I'm able to add new columns with LOC or without LOC. 
My question is why the below message coming while executing the syntax

E:\BIBEESH\PYTHON\NSE\Basan_Version_1.ipy:295: 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

On Tue, Jun 9, 2020 at 10:06 AM Jim Lareau <jtla...@gmail.com> wrote:
Yet again, a simple Google search:

pandas dataframe add column

yields as one result, the url:

Adding new column to existing DataFrame in Pandas - GeeksforGeeks
https://www.geeksforgeeks.org/adding-new-column-to-existing-dataframe-in-pandas/

Which answers your question.

Regards,

Jim

--
You received this message because you are subscribed to the Google Groups "spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spyd...@googlegroups.com.

Jim Lareau

unread,
Jun 11, 2020, 12:04:48 AM6/11/20
to spyder
Do you understand the purpose of the (new) warning message? I suggest doing a web search of:

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

Then reading the explanations on the Stack Overflow site (and within the pandas doc itself). This warning is about ambiguities in your code that might not work the way you think it will work. Using .loc is guaranteed to work the same way every time.

In your situation, the warning may or may not be a false positive... Reading the documentation and the comments from others will help you to determine which... It will also make you a better programmer as you learn to troubleshoot issues on your own. This is a process that every programmer goes through at some point in their career. The sooner that you learn how to do this, the faster your skills will develop.

Regards,

Jim

luc.k

unread,
Jun 11, 2020, 12:58:02 AM6/11/20
to spyder
Reply all
Reply to author
Forward
0 new messages