Python Pandas: Conditional Updating of Column Values Based on Another Col ('int' object is not subscriptable)...

Multi tool use
Multi tool use












0















I have the following sample data frame. col_2 is an integer datatype



col_1                 col_2
(hello, you, good) 65
(yes, are, tiles) 23


I want my code to replace the value in col_2 if col_1 contains a certain string. Otherwise, leave col_2 value as is.



Below is my code:



df['col_3'] = np.where(df['col_1'] == '(hello, you, good)', 18, df['col_2'])  


Below is my desired output:



col_1                 col_2
(hello, you, good) 18
(yes, are, tiles) 23


But when I run my code above, I get the following error:



TypeError: 'int' object is not subscriptable









share|improve this question

























  • Your code works for this sample (except it should be df['col_2']). Can you make it such that we can reproduce your problem?

    – coldspeed
    Jan 2 at 15:57











  • you are right; I made the col name edit

    – PineNuts0
    Jan 2 at 16:01






  • 1





    Your code still works for this example.

    – coldspeed
    Jan 2 at 16:03
















0















I have the following sample data frame. col_2 is an integer datatype



col_1                 col_2
(hello, you, good) 65
(yes, are, tiles) 23


I want my code to replace the value in col_2 if col_1 contains a certain string. Otherwise, leave col_2 value as is.



Below is my code:



df['col_3'] = np.where(df['col_1'] == '(hello, you, good)', 18, df['col_2'])  


Below is my desired output:



col_1                 col_2
(hello, you, good) 18
(yes, are, tiles) 23


But when I run my code above, I get the following error:



TypeError: 'int' object is not subscriptable









share|improve this question

























  • Your code works for this sample (except it should be df['col_2']). Can you make it such that we can reproduce your problem?

    – coldspeed
    Jan 2 at 15:57











  • you are right; I made the col name edit

    – PineNuts0
    Jan 2 at 16:01






  • 1





    Your code still works for this example.

    – coldspeed
    Jan 2 at 16:03














0












0








0








I have the following sample data frame. col_2 is an integer datatype



col_1                 col_2
(hello, you, good) 65
(yes, are, tiles) 23


I want my code to replace the value in col_2 if col_1 contains a certain string. Otherwise, leave col_2 value as is.



Below is my code:



df['col_3'] = np.where(df['col_1'] == '(hello, you, good)', 18, df['col_2'])  


Below is my desired output:



col_1                 col_2
(hello, you, good) 18
(yes, are, tiles) 23


But when I run my code above, I get the following error:



TypeError: 'int' object is not subscriptable









share|improve this question
















I have the following sample data frame. col_2 is an integer datatype



col_1                 col_2
(hello, you, good) 65
(yes, are, tiles) 23


I want my code to replace the value in col_2 if col_1 contains a certain string. Otherwise, leave col_2 value as is.



Below is my code:



df['col_3'] = np.where(df['col_1'] == '(hello, you, good)', 18, df['col_2'])  


Below is my desired output:



col_1                 col_2
(hello, you, good) 18
(yes, are, tiles) 23


But when I run my code above, I get the following error:



TypeError: 'int' object is not subscriptable






python pandas conditional where






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 16:01







PineNuts0

















asked Jan 2 at 15:56









PineNuts0PineNuts0

77121430




77121430













  • Your code works for this sample (except it should be df['col_2']). Can you make it such that we can reproduce your problem?

    – coldspeed
    Jan 2 at 15:57











  • you are right; I made the col name edit

    – PineNuts0
    Jan 2 at 16:01






  • 1





    Your code still works for this example.

    – coldspeed
    Jan 2 at 16:03



















  • Your code works for this sample (except it should be df['col_2']). Can you make it such that we can reproduce your problem?

    – coldspeed
    Jan 2 at 15:57











  • you are right; I made the col name edit

    – PineNuts0
    Jan 2 at 16:01






  • 1





    Your code still works for this example.

    – coldspeed
    Jan 2 at 16:03

















Your code works for this sample (except it should be df['col_2']). Can you make it such that we can reproduce your problem?

– coldspeed
Jan 2 at 15:57





Your code works for this sample (except it should be df['col_2']). Can you make it such that we can reproduce your problem?

– coldspeed
Jan 2 at 15:57













you are right; I made the col name edit

– PineNuts0
Jan 2 at 16:01





you are right; I made the col name edit

– PineNuts0
Jan 2 at 16:01




1




1





Your code still works for this example.

– coldspeed
Jan 2 at 16:03





Your code still works for this example.

– coldspeed
Jan 2 at 16:03












1 Answer
1






active

oldest

votes


















0














I guess that is not string type column for col_1



np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])  
Out[759]: array([18, 23], dtype=int64)





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%2f54009344%2fpython-pandas-conditional-updating-of-column-values-based-on-another-col-int%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














    I guess that is not string type column for col_1



    np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])  
    Out[759]: array([18, 23], dtype=int64)





    share|improve this answer




























      0














      I guess that is not string type column for col_1



      np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])  
      Out[759]: array([18, 23], dtype=int64)





      share|improve this answer


























        0












        0








        0







        I guess that is not string type column for col_1



        np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])  
        Out[759]: array([18, 23], dtype=int64)





        share|improve this answer













        I guess that is not string type column for col_1



        np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])  
        Out[759]: array([18, 23], dtype=int64)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 16:06









        Wen-BenWen-Ben

        117k83469




        117k83469
































            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%2f54009344%2fpython-pandas-conditional-updating-of-column-values-based-on-another-col-int%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







            676v7 j aQ3nvXG9dw MUKOIGday496jDVNpszLp2g,CMQqJMRGHf 35EU,e7szeLGhsDbkvT6,D95nIeo,76A,YIfL YFI3lXjLWIFjZJQf
            P,usiaV9rk,p wCNhLrUX m6HlI4a5v,7MaQL5pgQFG 9dP5T a0nXFFbWqYiB9ue6t

            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas