How to convert Pandas dataframe column into bin string data?











up vote
1
down vote

favorite












I have a Pandas dataframe called odf that looks like this:



Customer         Employees
A 2
B 100
C 5
D 1000


I have created custom bins for the employee data:



df = odf['Employees']
bins = [0,5,1000]
df.value_counts(bins=bins)

(-0.001, 5.0] 2
(5.0, 1000] 2
Name:Employees, dtype: int64


now I'd like to 'join' this data but am unsure how to do this, or if there is an easier way to accomplish what I need. I want the end result to look like this:



  Customer         Employees    NewBinColumn
A 2 -0.001, 5.0
B 100 5.0, 1000
C 5 -0.001, 5.0
D 1000 5.0, 1000


That way I can see the bin column next to the original dataframe columns



here is what I tried that did not work:



ndf = odf.join(df, lsuffix='Employees', rsuffix='Employees', how='left')
ndf


And while it does join the two, what I get is this:



  Customer         EmployeesEmployees    Employees
A 2 2
B 100 100
C 5 5
D 1000 1000


If this was SQL I'd use a case statement to get the new column, but I was hoping there is an easier way to dynamically do this without writing out a really long statement.










share|improve this question


























    up vote
    1
    down vote

    favorite












    I have a Pandas dataframe called odf that looks like this:



    Customer         Employees
    A 2
    B 100
    C 5
    D 1000


    I have created custom bins for the employee data:



    df = odf['Employees']
    bins = [0,5,1000]
    df.value_counts(bins=bins)

    (-0.001, 5.0] 2
    (5.0, 1000] 2
    Name:Employees, dtype: int64


    now I'd like to 'join' this data but am unsure how to do this, or if there is an easier way to accomplish what I need. I want the end result to look like this:



      Customer         Employees    NewBinColumn
    A 2 -0.001, 5.0
    B 100 5.0, 1000
    C 5 -0.001, 5.0
    D 1000 5.0, 1000


    That way I can see the bin column next to the original dataframe columns



    here is what I tried that did not work:



    ndf = odf.join(df, lsuffix='Employees', rsuffix='Employees', how='left')
    ndf


    And while it does join the two, what I get is this:



      Customer         EmployeesEmployees    Employees
    A 2 2
    B 100 100
    C 5 5
    D 1000 1000


    If this was SQL I'd use a case statement to get the new column, but I was hoping there is an easier way to dynamically do this without writing out a really long statement.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a Pandas dataframe called odf that looks like this:



      Customer         Employees
      A 2
      B 100
      C 5
      D 1000


      I have created custom bins for the employee data:



      df = odf['Employees']
      bins = [0,5,1000]
      df.value_counts(bins=bins)

      (-0.001, 5.0] 2
      (5.0, 1000] 2
      Name:Employees, dtype: int64


      now I'd like to 'join' this data but am unsure how to do this, or if there is an easier way to accomplish what I need. I want the end result to look like this:



        Customer         Employees    NewBinColumn
      A 2 -0.001, 5.0
      B 100 5.0, 1000
      C 5 -0.001, 5.0
      D 1000 5.0, 1000


      That way I can see the bin column next to the original dataframe columns



      here is what I tried that did not work:



      ndf = odf.join(df, lsuffix='Employees', rsuffix='Employees', how='left')
      ndf


      And while it does join the two, what I get is this:



        Customer         EmployeesEmployees    Employees
      A 2 2
      B 100 100
      C 5 5
      D 1000 1000


      If this was SQL I'd use a case statement to get the new column, but I was hoping there is an easier way to dynamically do this without writing out a really long statement.










      share|improve this question













      I have a Pandas dataframe called odf that looks like this:



      Customer         Employees
      A 2
      B 100
      C 5
      D 1000


      I have created custom bins for the employee data:



      df = odf['Employees']
      bins = [0,5,1000]
      df.value_counts(bins=bins)

      (-0.001, 5.0] 2
      (5.0, 1000] 2
      Name:Employees, dtype: int64


      now I'd like to 'join' this data but am unsure how to do this, or if there is an easier way to accomplish what I need. I want the end result to look like this:



        Customer         Employees    NewBinColumn
      A 2 -0.001, 5.0
      B 100 5.0, 1000
      C 5 -0.001, 5.0
      D 1000 5.0, 1000


      That way I can see the bin column next to the original dataframe columns



      here is what I tried that did not work:



      ndf = odf.join(df, lsuffix='Employees', rsuffix='Employees', how='left')
      ndf


      And while it does join the two, what I get is this:



        Customer         EmployeesEmployees    Employees
      A 2 2
      B 100 100
      C 5 5
      D 1000 1000


      If this was SQL I'd use a case statement to get the new column, but I was hoping there is an easier way to dynamically do this without writing out a really long statement.







      pandas dataframe join bins






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 19:45









      user76595

      728




      728
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          It is not exactly the same formating that what you want, but using pd.cut on odf['Employees'] such as:



          odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)


          will give:



            Customer  Employees NewBinColumn
          0 A 2 (0, 5]
          1 B 100 (5, 1000]
          2 C 5 (0, 5]
          3 D 1000 (5, 1000]





          share|improve this answer

















          • 1




            This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
            – user76595
            Nov 9 at 20:07











          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',
          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%2f53232347%2fhow-to-convert-pandas-dataframe-column-into-bin-string-data%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








          up vote
          1
          down vote



          accepted










          It is not exactly the same formating that what you want, but using pd.cut on odf['Employees'] such as:



          odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)


          will give:



            Customer  Employees NewBinColumn
          0 A 2 (0, 5]
          1 B 100 (5, 1000]
          2 C 5 (0, 5]
          3 D 1000 (5, 1000]





          share|improve this answer

















          • 1




            This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
            – user76595
            Nov 9 at 20:07















          up vote
          1
          down vote



          accepted










          It is not exactly the same formating that what you want, but using pd.cut on odf['Employees'] such as:



          odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)


          will give:



            Customer  Employees NewBinColumn
          0 A 2 (0, 5]
          1 B 100 (5, 1000]
          2 C 5 (0, 5]
          3 D 1000 (5, 1000]





          share|improve this answer

















          • 1




            This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
            – user76595
            Nov 9 at 20:07













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          It is not exactly the same formating that what you want, but using pd.cut on odf['Employees'] such as:



          odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)


          will give:



            Customer  Employees NewBinColumn
          0 A 2 (0, 5]
          1 B 100 (5, 1000]
          2 C 5 (0, 5]
          3 D 1000 (5, 1000]





          share|improve this answer












          It is not exactly the same formating that what you want, but using pd.cut on odf['Employees'] such as:



          odf['NewBinColumn'] = pd.cut(odf['Employees'],bins)


          will give:



            Customer  Employees NewBinColumn
          0 A 2 (0, 5]
          1 B 100 (5, 1000]
          2 C 5 (0, 5]
          3 D 1000 (5, 1000]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 19:58









          Ben.T

          5,4822523




          5,4822523








          • 1




            This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
            – user76595
            Nov 9 at 20:07














          • 1




            This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
            – user76595
            Nov 9 at 20:07








          1




          1




          This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
          – user76595
          Nov 9 at 20:07




          This is close enough. I'm newb enough to handle the formatting after the fact. I just tend to make things more complicated than need be. Thanks again.
          – user76595
          Nov 9 at 20:07


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53232347%2fhow-to-convert-pandas-dataframe-column-into-bin-string-data%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()