How to count occurrences of values of a list in a column of a different dataframe?How do I check if a list is empty?What is the difference between Python's list methods append and extend?How do I sort a dictionary by value?How to make a flat list out of list of listsHow can I count the occurrences of a list item?How to clone or copy a list?How do I list all files of a directory?Delete column from pandas DataFrame“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandas

In the 3D Zeldas, is it faster to roll or to simply walk?

Why were helmets and other body armour not commonplace in the 1800s?

Where have Brexit voters gone?

What does $!# mean in Shell scripting?

Why isn't 'chemically-strengthened glass' made with potassium carbonate to begin with?

role of -られ, -し, and construction of the phrase

I know that there is a preselected candidate for a position to be filled at my department. What should I do?

How to respond to upset student?

Why aren't space telescopes put in GEO?

What is the function of the corrugations on a section of the Space Shuttle's external tank?

Did 20% of US soldiers in Vietnam use heroin, 95% of whom quit afterwards?

Compaq Portable vs IBM 5155 Portable PC

Make 24 using exactly three 3s

My employer faked my resume to acquire projects

Is it truly impossible to tell what a CPU is doing?

How to politely tell someone they did not hit "reply to all" in an email?

Is there an online tool which supports shared writing?

What could a self-sustaining lunar colony slowly lose that would ultimately prove fatal?

Is it legal to meet with potential future employers in the UK, whilst visiting from the USA

First Match - awk

Is it true that cut time means "play twice as fast as written"?

Can I summon an otherworldly creature with the Gate spell without knowing its true name?

How can I tell if I'm being too picky as a referee?

Why did Jon Snow do this immoral act if he is so honorable?



How to count occurrences of values of a list in a column of a different dataframe?


How do I check if a list is empty?What is the difference between Python's list methods append and extend?How do I sort a dictionary by value?How to make a flat list out of list of listsHow can I count the occurrences of a list item?How to clone or copy a list?How do I list all files of a directory?Delete column from pandas DataFrame“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandas






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















I need to count the number of times the values of a list appear in a column of a different DataFrame.



I've tried using df['Column'].value_counts().
However,if a value is in the list but not in the column, it won't show up in the result.



df = pd.DataFrame('Column': [HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW])

list = ['HIGH', 'MEDIUM', 'LOW']


I expect the output to be:



HIGH 3
MEDIUM 0
LOW 4


But using .value_counts() I get:



HIGH 3
LOW 4


Can someone please point out how I can achieve this? Is it possible to write a piece of code so that my output will always show counts for HIGH, MEDIUM and LOW, regardless of how the data is distributed? (The next time I load the data, it might be possible that there are no HIGH values, instead of MEDIUM)










share|improve this question






























    3















    I need to count the number of times the values of a list appear in a column of a different DataFrame.



    I've tried using df['Column'].value_counts().
    However,if a value is in the list but not in the column, it won't show up in the result.



    df = pd.DataFrame('Column': [HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW])

    list = ['HIGH', 'MEDIUM', 'LOW']


    I expect the output to be:



    HIGH 3
    MEDIUM 0
    LOW 4


    But using .value_counts() I get:



    HIGH 3
    LOW 4


    Can someone please point out how I can achieve this? Is it possible to write a piece of code so that my output will always show counts for HIGH, MEDIUM and LOW, regardless of how the data is distributed? (The next time I load the data, it might be possible that there are no HIGH values, instead of MEDIUM)










    share|improve this question


























      3












      3








      3








      I need to count the number of times the values of a list appear in a column of a different DataFrame.



      I've tried using df['Column'].value_counts().
      However,if a value is in the list but not in the column, it won't show up in the result.



      df = pd.DataFrame('Column': [HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW])

      list = ['HIGH', 'MEDIUM', 'LOW']


      I expect the output to be:



      HIGH 3
      MEDIUM 0
      LOW 4


      But using .value_counts() I get:



      HIGH 3
      LOW 4


      Can someone please point out how I can achieve this? Is it possible to write a piece of code so that my output will always show counts for HIGH, MEDIUM and LOW, regardless of how the data is distributed? (The next time I load the data, it might be possible that there are no HIGH values, instead of MEDIUM)










      share|improve this question
















      I need to count the number of times the values of a list appear in a column of a different DataFrame.



      I've tried using df['Column'].value_counts().
      However,if a value is in the list but not in the column, it won't show up in the result.



      df = pd.DataFrame('Column': [HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW])

      list = ['HIGH', 'MEDIUM', 'LOW']


      I expect the output to be:



      HIGH 3
      MEDIUM 0
      LOW 4


      But using .value_counts() I get:



      HIGH 3
      LOW 4


      Can someone please point out how I can achieve this? Is it possible to write a piece of code so that my output will always show counts for HIGH, MEDIUM and LOW, regardless of how the data is distributed? (The next time I load the data, it might be possible that there are no HIGH values, instead of MEDIUM)







      python pandas group-by jupyter-notebook






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 24 at 3:31









      ALollz

      18.6k51840




      18.6k51840










      asked Mar 24 at 2:45









      pd123pd123

      183




      183






















          2 Answers
          2






          active

          oldest

          votes


















          7














          One quick fix reindex



          df.Column.value_counts().reindex(list,fill_value=0)
          HIGH 3
          MEDIUM 0
          LOW 4
          Name: Column, dtype: int64



          Another way pd.Categorical



          pd.Categorical(df.Column,list).value_counts()
          HIGH 3
          MEDIUM 0
          LOW 4
          dtype: int64





          share|improve this answer






























            0














            Try below, it creates an empty series first and then combines it with value_counts series:



            pd.Series(0, index=list).combine(df.value_counts(), max)






            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%2f55320303%2fhow-to-count-occurrences-of-values-of-a-list-in-a-column-of-a-different-datafram%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              One quick fix reindex



              df.Column.value_counts().reindex(list,fill_value=0)
              HIGH 3
              MEDIUM 0
              LOW 4
              Name: Column, dtype: int64



              Another way pd.Categorical



              pd.Categorical(df.Column,list).value_counts()
              HIGH 3
              MEDIUM 0
              LOW 4
              dtype: int64





              share|improve this answer



























                7














                One quick fix reindex



                df.Column.value_counts().reindex(list,fill_value=0)
                HIGH 3
                MEDIUM 0
                LOW 4
                Name: Column, dtype: int64



                Another way pd.Categorical



                pd.Categorical(df.Column,list).value_counts()
                HIGH 3
                MEDIUM 0
                LOW 4
                dtype: int64





                share|improve this answer

























                  7












                  7








                  7







                  One quick fix reindex



                  df.Column.value_counts().reindex(list,fill_value=0)
                  HIGH 3
                  MEDIUM 0
                  LOW 4
                  Name: Column, dtype: int64



                  Another way pd.Categorical



                  pd.Categorical(df.Column,list).value_counts()
                  HIGH 3
                  MEDIUM 0
                  LOW 4
                  dtype: int64





                  share|improve this answer













                  One quick fix reindex



                  df.Column.value_counts().reindex(list,fill_value=0)
                  HIGH 3
                  MEDIUM 0
                  LOW 4
                  Name: Column, dtype: int64



                  Another way pd.Categorical



                  pd.Categorical(df.Column,list).value_counts()
                  HIGH 3
                  MEDIUM 0
                  LOW 4
                  dtype: int64






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 24 at 2:48









                  WeNYoBenWeNYoBen

                  136k84574




                  136k84574























                      0














                      Try below, it creates an empty series first and then combines it with value_counts series:



                      pd.Series(0, index=list).combine(df.value_counts(), max)






                      share|improve this answer



























                        0














                        Try below, it creates an empty series first and then combines it with value_counts series:



                        pd.Series(0, index=list).combine(df.value_counts(), max)






                        share|improve this answer

























                          0












                          0








                          0







                          Try below, it creates an empty series first and then combines it with value_counts series:



                          pd.Series(0, index=list).combine(df.value_counts(), max)






                          share|improve this answer













                          Try below, it creates an empty series first and then combines it with value_counts series:



                          pd.Series(0, index=list).combine(df.value_counts(), max)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 24 at 7:34









                          hacker315hacker315

                          1,267715




                          1,267715



























                              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%2f55320303%2fhow-to-count-occurrences-of-values-of-a-list-in-a-column-of-a-different-datafram%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

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript