Python/Pandas/Matplotlib - Iterating/Looping Through Charts - Duplicate Charts Issue












0















Trying to produce one chart for every 'query' with the 'keyword' on the x-axis and the 'trend' value on my y-axis.



This is my dataframe, called 'df'.



                   query    keyword                     trend   sequence
1592 womens underwear women's underwear on sale 1.40 1
1593 womens underwear sale on womens underwear 1.40 2
1594 womens underwear women's underwear for sale 1.40 3
1595 womens underwear womens underwear sale 1.40 4
3687 womens underwear womens underwear sale 1.40 5
3802 womens bra lingerie sleepwear 124.00 1
1705 womens bra women's n i t bracket 3.00 2
3852 womens bra best bras for women 2.22 3
3826 womens bra fantasy bra 1.75 4
3851 womens bra 32 c bra 1.71 5


What is happening is that I am producing 'n' charts in my for loop for each query. Many duplicates!



for i in df['query']:
loopChart = df[df['query']==i]
plt.figure(figsize=(10,4))
plt.bar(loopChart['keyword'], loopChart['trend'])
plt.title(i)
plt.xticks(rotation=90)
plt.show()


I have tried a few things here with no luck:



1) Creating a sequence field (in my DF example) and using that to limit the output in my loopChart line



loopChart = df[df['query']==i & df['sequence']==1]



2) Using my sequence field as a nested while loop criteria



for i in df['query']:



while df['sequence'] <2:



Any insights on how else I might approach this challenge? Tried to think of ways to only go through and chart each 'query' once, but am stumped.



My end result should just be two bar charts, but right now it is 10.










share|improve this question



























    0















    Trying to produce one chart for every 'query' with the 'keyword' on the x-axis and the 'trend' value on my y-axis.



    This is my dataframe, called 'df'.



                       query    keyword                     trend   sequence
    1592 womens underwear women's underwear on sale 1.40 1
    1593 womens underwear sale on womens underwear 1.40 2
    1594 womens underwear women's underwear for sale 1.40 3
    1595 womens underwear womens underwear sale 1.40 4
    3687 womens underwear womens underwear sale 1.40 5
    3802 womens bra lingerie sleepwear 124.00 1
    1705 womens bra women's n i t bracket 3.00 2
    3852 womens bra best bras for women 2.22 3
    3826 womens bra fantasy bra 1.75 4
    3851 womens bra 32 c bra 1.71 5


    What is happening is that I am producing 'n' charts in my for loop for each query. Many duplicates!



    for i in df['query']:
    loopChart = df[df['query']==i]
    plt.figure(figsize=(10,4))
    plt.bar(loopChart['keyword'], loopChart['trend'])
    plt.title(i)
    plt.xticks(rotation=90)
    plt.show()


    I have tried a few things here with no luck:



    1) Creating a sequence field (in my DF example) and using that to limit the output in my loopChart line



    loopChart = df[df['query']==i & df['sequence']==1]



    2) Using my sequence field as a nested while loop criteria



    for i in df['query']:



    while df['sequence'] <2:



    Any insights on how else I might approach this challenge? Tried to think of ways to only go through and chart each 'query' once, but am stumped.



    My end result should just be two bar charts, but right now it is 10.










    share|improve this question

























      0












      0








      0








      Trying to produce one chart for every 'query' with the 'keyword' on the x-axis and the 'trend' value on my y-axis.



      This is my dataframe, called 'df'.



                         query    keyword                     trend   sequence
      1592 womens underwear women's underwear on sale 1.40 1
      1593 womens underwear sale on womens underwear 1.40 2
      1594 womens underwear women's underwear for sale 1.40 3
      1595 womens underwear womens underwear sale 1.40 4
      3687 womens underwear womens underwear sale 1.40 5
      3802 womens bra lingerie sleepwear 124.00 1
      1705 womens bra women's n i t bracket 3.00 2
      3852 womens bra best bras for women 2.22 3
      3826 womens bra fantasy bra 1.75 4
      3851 womens bra 32 c bra 1.71 5


      What is happening is that I am producing 'n' charts in my for loop for each query. Many duplicates!



      for i in df['query']:
      loopChart = df[df['query']==i]
      plt.figure(figsize=(10,4))
      plt.bar(loopChart['keyword'], loopChart['trend'])
      plt.title(i)
      plt.xticks(rotation=90)
      plt.show()


      I have tried a few things here with no luck:



      1) Creating a sequence field (in my DF example) and using that to limit the output in my loopChart line



      loopChart = df[df['query']==i & df['sequence']==1]



      2) Using my sequence field as a nested while loop criteria



      for i in df['query']:



      while df['sequence'] <2:



      Any insights on how else I might approach this challenge? Tried to think of ways to only go through and chart each 'query' once, but am stumped.



      My end result should just be two bar charts, but right now it is 10.










      share|improve this question














      Trying to produce one chart for every 'query' with the 'keyword' on the x-axis and the 'trend' value on my y-axis.



      This is my dataframe, called 'df'.



                         query    keyword                     trend   sequence
      1592 womens underwear women's underwear on sale 1.40 1
      1593 womens underwear sale on womens underwear 1.40 2
      1594 womens underwear women's underwear for sale 1.40 3
      1595 womens underwear womens underwear sale 1.40 4
      3687 womens underwear womens underwear sale 1.40 5
      3802 womens bra lingerie sleepwear 124.00 1
      1705 womens bra women's n i t bracket 3.00 2
      3852 womens bra best bras for women 2.22 3
      3826 womens bra fantasy bra 1.75 4
      3851 womens bra 32 c bra 1.71 5


      What is happening is that I am producing 'n' charts in my for loop for each query. Many duplicates!



      for i in df['query']:
      loopChart = df[df['query']==i]
      plt.figure(figsize=(10,4))
      plt.bar(loopChart['keyword'], loopChart['trend'])
      plt.title(i)
      plt.xticks(rotation=90)
      plt.show()


      I have tried a few things here with no luck:



      1) Creating a sequence field (in my DF example) and using that to limit the output in my loopChart line



      loopChart = df[df['query']==i & df['sequence']==1]



      2) Using my sequence field as a nested while loop criteria



      for i in df['query']:



      while df['sequence'] <2:



      Any insights on how else I might approach this challenge? Tried to think of ways to only go through and chart each 'query' once, but am stumped.



      My end result should just be two bar charts, but right now it is 10.







      python pandas for-loop matplotlib






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 21:01









      Sandy McAllisterSandy McAllister

      153




      153
























          1 Answer
          1






          active

          oldest

          votes


















          0














          disregard - I solved the issue with a small bit of code...added a .unique() to my for loop header and it all worked out.



          for i in df['query'].unique():
          loopChart = df[df['query']==i]
          plt.figure(figsize=(10,4))
          plt.bar(loopChart['keyword'], loopChart['trend'])
          plt.title(i)
          plt.xticks(rotation=90)
          plt.show()





          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%2f53998915%2fpython-pandas-matplotlib-iterating-looping-through-charts-duplicate-charts-i%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














            disregard - I solved the issue with a small bit of code...added a .unique() to my for loop header and it all worked out.



            for i in df['query'].unique():
            loopChart = df[df['query']==i]
            plt.figure(figsize=(10,4))
            plt.bar(loopChart['keyword'], loopChart['trend'])
            plt.title(i)
            plt.xticks(rotation=90)
            plt.show()





            share|improve this answer




























              0














              disregard - I solved the issue with a small bit of code...added a .unique() to my for loop header and it all worked out.



              for i in df['query'].unique():
              loopChart = df[df['query']==i]
              plt.figure(figsize=(10,4))
              plt.bar(loopChart['keyword'], loopChart['trend'])
              plt.title(i)
              plt.xticks(rotation=90)
              plt.show()





              share|improve this answer


























                0












                0








                0







                disregard - I solved the issue with a small bit of code...added a .unique() to my for loop header and it all worked out.



                for i in df['query'].unique():
                loopChart = df[df['query']==i]
                plt.figure(figsize=(10,4))
                plt.bar(loopChart['keyword'], loopChart['trend'])
                plt.title(i)
                plt.xticks(rotation=90)
                plt.show()





                share|improve this answer













                disregard - I solved the issue with a small bit of code...added a .unique() to my for loop header and it all worked out.



                for i in df['query'].unique():
                loopChart = df[df['query']==i]
                plt.figure(figsize=(10,4))
                plt.bar(loopChart['keyword'], loopChart['trend'])
                plt.title(i)
                plt.xticks(rotation=90)
                plt.show()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 21:04









                Sandy McAllisterSandy McAllister

                153




                153
































                    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%2f53998915%2fpython-pandas-matplotlib-iterating-looping-through-charts-duplicate-charts-i%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

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    Npm cannot find a required file even through it is in the searched directory