Using df['C'] vs. df.loc[:, 'C'] to assign new column in Pandas dataframe












-1















I have a dataframe:



df = pd.DataFrame({'A':np.random.randint(1,10, 10), 'B':np.random.randint(1,10, 10)})

def sumf(row):
result = None
if row['A']>= row['B']:
result = row['A'] - row['B']
else:
result = row['B'] - row['A']
return result

df.loc[:,'C'] = df.apply(sumf, axis = 1)
df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var


What would be the difference in terms of view/copy for column C and D? And is it the right way to fill column E with zero?
I have a similar data frame with the same data and logic (just in another jupyter notebook), but there I am getting a warning:




/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:19:
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




When I try these lines:



df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var









share|improve this question

























  • I dont get this error. Is there another piece of code your are running that's causing this error upstream?

    – DJK
    Nov 21 '18 at 15:52











  • Sorry, didn't figure out how to post formatted code as a comment. So my production code is the same (except column names, type of calculation and the data). And I don't get the warning also (when I run the code above) but I do get this warning when I run my production code.

    – Ildar Gabdrakhmanov
    Nov 21 '18 at 16:10
















-1















I have a dataframe:



df = pd.DataFrame({'A':np.random.randint(1,10, 10), 'B':np.random.randint(1,10, 10)})

def sumf(row):
result = None
if row['A']>= row['B']:
result = row['A'] - row['B']
else:
result = row['B'] - row['A']
return result

df.loc[:,'C'] = df.apply(sumf, axis = 1)
df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var


What would be the difference in terms of view/copy for column C and D? And is it the right way to fill column E with zero?
I have a similar data frame with the same data and logic (just in another jupyter notebook), but there I am getting a warning:




/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:19:
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




When I try these lines:



df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var









share|improve this question

























  • I dont get this error. Is there another piece of code your are running that's causing this error upstream?

    – DJK
    Nov 21 '18 at 15:52











  • Sorry, didn't figure out how to post formatted code as a comment. So my production code is the same (except column names, type of calculation and the data). And I don't get the warning also (when I run the code above) but I do get this warning when I run my production code.

    – Ildar Gabdrakhmanov
    Nov 21 '18 at 16:10














-1












-1








-1








I have a dataframe:



df = pd.DataFrame({'A':np.random.randint(1,10, 10), 'B':np.random.randint(1,10, 10)})

def sumf(row):
result = None
if row['A']>= row['B']:
result = row['A'] - row['B']
else:
result = row['B'] - row['A']
return result

df.loc[:,'C'] = df.apply(sumf, axis = 1)
df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var


What would be the difference in terms of view/copy for column C and D? And is it the right way to fill column E with zero?
I have a similar data frame with the same data and logic (just in another jupyter notebook), but there I am getting a warning:




/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:19:
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




When I try these lines:



df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var









share|improve this question
















I have a dataframe:



df = pd.DataFrame({'A':np.random.randint(1,10, 10), 'B':np.random.randint(1,10, 10)})

def sumf(row):
result = None
if row['A']>= row['B']:
result = row['A'] - row['B']
else:
result = row['B'] - row['A']
return result

df.loc[:,'C'] = df.apply(sumf, axis = 1)
df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var


What would be the difference in terms of view/copy for column C and D? And is it the right way to fill column E with zero?
I have a similar data frame with the same data and logic (just in another jupyter notebook), but there I am getting a warning:




/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:19:
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




When I try these lines:



df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var






python-3.x pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 16:46









PJW

740826




740826










asked Nov 21 '18 at 15:44









Ildar GabdrakhmanovIldar Gabdrakhmanov

7310




7310













  • I dont get this error. Is there another piece of code your are running that's causing this error upstream?

    – DJK
    Nov 21 '18 at 15:52











  • Sorry, didn't figure out how to post formatted code as a comment. So my production code is the same (except column names, type of calculation and the data). And I don't get the warning also (when I run the code above) but I do get this warning when I run my production code.

    – Ildar Gabdrakhmanov
    Nov 21 '18 at 16:10



















  • I dont get this error. Is there another piece of code your are running that's causing this error upstream?

    – DJK
    Nov 21 '18 at 15:52











  • Sorry, didn't figure out how to post formatted code as a comment. So my production code is the same (except column names, type of calculation and the data). And I don't get the warning also (when I run the code above) but I do get this warning when I run my production code.

    – Ildar Gabdrakhmanov
    Nov 21 '18 at 16:10

















I dont get this error. Is there another piece of code your are running that's causing this error upstream?

– DJK
Nov 21 '18 at 15:52





I dont get this error. Is there another piece of code your are running that's causing this error upstream?

– DJK
Nov 21 '18 at 15:52













Sorry, didn't figure out how to post formatted code as a comment. So my production code is the same (except column names, type of calculation and the data). And I don't get the warning also (when I run the code above) but I do get this warning when I run my production code.

– Ildar Gabdrakhmanov
Nov 21 '18 at 16:10





Sorry, didn't figure out how to post formatted code as a comment. So my production code is the same (except column names, type of calculation and the data). And I don't get the warning also (when I run the code above) but I do get this warning when I run my production code.

– Ildar Gabdrakhmanov
Nov 21 '18 at 16:10












1 Answer
1






active

oldest

votes


















0














The SettingWithCopyWarning is a warning related to the possibility of chained assignment. From the docs on "Returning a view versus a copy", it states "The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid assignment. There may be false positives; situations where a chained assignment is inadvertently reported."



I am not able to reproduce this warning when running your code (with pandas==0.23.4 and Python 2.7.15). Possibly you are running a different version of pandas? This post seems to indicate that this is a pandas version issue. Likely if you upgrade your pandas version, the warning will not appear, and both ways to assign a new column (df.loc[:,'C'] or df['C']) are valid. Make sure your versions of python, pandas and numpy are upgraded and the same in your different environments.



In your case, the warning is certainly a false-positive situation, since you are defining new columns in your original dataframe (not using a copy of the dataframe).



And, yes, if you want to fill column E with the string zero, this is an appropriate way to do so.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415682%2fusing-dfc-vs-df-loc-c-to-assign-new-column-in-pandas-dataframe%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The SettingWithCopyWarning is a warning related to the possibility of chained assignment. From the docs on "Returning a view versus a copy", it states "The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid assignment. There may be false positives; situations where a chained assignment is inadvertently reported."



    I am not able to reproduce this warning when running your code (with pandas==0.23.4 and Python 2.7.15). Possibly you are running a different version of pandas? This post seems to indicate that this is a pandas version issue. Likely if you upgrade your pandas version, the warning will not appear, and both ways to assign a new column (df.loc[:,'C'] or df['C']) are valid. Make sure your versions of python, pandas and numpy are upgraded and the same in your different environments.



    In your case, the warning is certainly a false-positive situation, since you are defining new columns in your original dataframe (not using a copy of the dataframe).



    And, yes, if you want to fill column E with the string zero, this is an appropriate way to do so.






    share|improve this answer






























      0














      The SettingWithCopyWarning is a warning related to the possibility of chained assignment. From the docs on "Returning a view versus a copy", it states "The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid assignment. There may be false positives; situations where a chained assignment is inadvertently reported."



      I am not able to reproduce this warning when running your code (with pandas==0.23.4 and Python 2.7.15). Possibly you are running a different version of pandas? This post seems to indicate that this is a pandas version issue. Likely if you upgrade your pandas version, the warning will not appear, and both ways to assign a new column (df.loc[:,'C'] or df['C']) are valid. Make sure your versions of python, pandas and numpy are upgraded and the same in your different environments.



      In your case, the warning is certainly a false-positive situation, since you are defining new columns in your original dataframe (not using a copy of the dataframe).



      And, yes, if you want to fill column E with the string zero, this is an appropriate way to do so.






      share|improve this answer




























        0












        0








        0







        The SettingWithCopyWarning is a warning related to the possibility of chained assignment. From the docs on "Returning a view versus a copy", it states "The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid assignment. There may be false positives; situations where a chained assignment is inadvertently reported."



        I am not able to reproduce this warning when running your code (with pandas==0.23.4 and Python 2.7.15). Possibly you are running a different version of pandas? This post seems to indicate that this is a pandas version issue. Likely if you upgrade your pandas version, the warning will not appear, and both ways to assign a new column (df.loc[:,'C'] or df['C']) are valid. Make sure your versions of python, pandas and numpy are upgraded and the same in your different environments.



        In your case, the warning is certainly a false-positive situation, since you are defining new columns in your original dataframe (not using a copy of the dataframe).



        And, yes, if you want to fill column E with the string zero, this is an appropriate way to do so.






        share|improve this answer















        The SettingWithCopyWarning is a warning related to the possibility of chained assignment. From the docs on "Returning a view versus a copy", it states "The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid assignment. There may be false positives; situations where a chained assignment is inadvertently reported."



        I am not able to reproduce this warning when running your code (with pandas==0.23.4 and Python 2.7.15). Possibly you are running a different version of pandas? This post seems to indicate that this is a pandas version issue. Likely if you upgrade your pandas version, the warning will not appear, and both ways to assign a new column (df.loc[:,'C'] or df['C']) are valid. Make sure your versions of python, pandas and numpy are upgraded and the same in your different environments.



        In your case, the warning is certainly a false-positive situation, since you are defining new columns in your original dataframe (not using a copy of the dataframe).



        And, yes, if you want to fill column E with the string zero, this is an appropriate way to do so.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 16:44

























        answered Nov 21 '18 at 16:29









        PJWPJW

        740826




        740826
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415682%2fusing-dfc-vs-df-loc-c-to-assign-new-column-in-pandas-dataframe%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?