Create a dict of dict with 2 columns and with values from a third column in pandas












1















I have a dataframe in python that looks like somewhat following:



df = pd.DataFrame({
'tag':['php','image-processing','file-upload','upload','mime-types'] * 2,
'probability':np.arange(10),
'token':['check'] * 5 + ['imag'] * 5
}).set_index(['tag','token'])
print (df)
probability
tag token
php check 0
image-processing check 1
file-upload check 2
upload check 3
mime-types check 4
php imag 5
image-processing imag 6
file-upload imag 7
upload imag 8
mime-types imag 9


I need to crate a dicts of dicts and the values being probability. For example for php



{'php': {'check': 0,
'imag': 1,
....},
'image-processing': {....},
'file-upload': {....},

}


I don't need the internal dicts in any order of the probability values. I can code this up easily but I need to know if there is any pandas trick that can be applied here.










share|improve this question





























    1















    I have a dataframe in python that looks like somewhat following:



    df = pd.DataFrame({
    'tag':['php','image-processing','file-upload','upload','mime-types'] * 2,
    'probability':np.arange(10),
    'token':['check'] * 5 + ['imag'] * 5
    }).set_index(['tag','token'])
    print (df)
    probability
    tag token
    php check 0
    image-processing check 1
    file-upload check 2
    upload check 3
    mime-types check 4
    php imag 5
    image-processing imag 6
    file-upload imag 7
    upload imag 8
    mime-types imag 9


    I need to crate a dicts of dicts and the values being probability. For example for php



    {'php': {'check': 0,
    'imag': 1,
    ....},
    'image-processing': {....},
    'file-upload': {....},

    }


    I don't need the internal dicts in any order of the probability values. I can code this up easily but I need to know if there is any pandas trick that can be applied here.










    share|improve this question



























      1












      1








      1








      I have a dataframe in python that looks like somewhat following:



      df = pd.DataFrame({
      'tag':['php','image-processing','file-upload','upload','mime-types'] * 2,
      'probability':np.arange(10),
      'token':['check'] * 5 + ['imag'] * 5
      }).set_index(['tag','token'])
      print (df)
      probability
      tag token
      php check 0
      image-processing check 1
      file-upload check 2
      upload check 3
      mime-types check 4
      php imag 5
      image-processing imag 6
      file-upload imag 7
      upload imag 8
      mime-types imag 9


      I need to crate a dicts of dicts and the values being probability. For example for php



      {'php': {'check': 0,
      'imag': 1,
      ....},
      'image-processing': {....},
      'file-upload': {....},

      }


      I don't need the internal dicts in any order of the probability values. I can code this up easily but I need to know if there is any pandas trick that can be applied here.










      share|improve this question
















      I have a dataframe in python that looks like somewhat following:



      df = pd.DataFrame({
      'tag':['php','image-processing','file-upload','upload','mime-types'] * 2,
      'probability':np.arange(10),
      'token':['check'] * 5 + ['imag'] * 5
      }).set_index(['tag','token'])
      print (df)
      probability
      tag token
      php check 0
      image-processing check 1
      file-upload check 2
      upload check 3
      mime-types check 4
      php imag 5
      image-processing imag 6
      file-upload imag 7
      upload imag 8
      mime-types imag 9


      I need to crate a dicts of dicts and the values being probability. For example for php



      {'php': {'check': 0,
      'imag': 1,
      ....},
      'image-processing': {....},
      'file-upload': {....},

      }


      I don't need the internal dicts in any order of the probability values. I can code this up easily but I need to know if there is any pandas trick that can be applied here.







      python pandas dictionary






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 11:17









      jezrael

      340k25294365




      340k25294365










      asked Nov 20 '18 at 10:54









      Mayukh SarkarMayukh Sarkar

      915821




      915821
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Use dictionary comprehension:



          d = {k:v.reset_index(level=0, drop=True).to_dict() 
          for k, v in df.groupby(level=0)['probability']}


          Another solution:



          d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}




          print (d)

          {'file-upload': {'check': 2, 'imag': 7},
          'image-processing': {'check': 1, 'imag': 6},
          'mime-types': {'check': 4, 'imag': 9},
          'php': {'check': 0, 'imag': 5},
          'upload': {'check': 3, 'imag': 8}}





          share|improve this answer

































            0














            I think this is the fastest method:



            Your example:



            df.reset_index(level=1, inplace=True)
            df_dict = df.to_dict('your_index')


            Simple example:



            import pandas as pd
            df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
            df2.to_dict('id')


            pandas.DataFrame.to_dict






            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%2f53391438%2fcreate-a-dict-of-dict-with-2-columns-and-with-values-from-a-third-column-in-pand%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









              2














              Use dictionary comprehension:



              d = {k:v.reset_index(level=0, drop=True).to_dict() 
              for k, v in df.groupby(level=0)['probability']}


              Another solution:



              d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}




              print (d)

              {'file-upload': {'check': 2, 'imag': 7},
              'image-processing': {'check': 1, 'imag': 6},
              'mime-types': {'check': 4, 'imag': 9},
              'php': {'check': 0, 'imag': 5},
              'upload': {'check': 3, 'imag': 8}}





              share|improve this answer






























                2














                Use dictionary comprehension:



                d = {k:v.reset_index(level=0, drop=True).to_dict() 
                for k, v in df.groupby(level=0)['probability']}


                Another solution:



                d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}




                print (d)

                {'file-upload': {'check': 2, 'imag': 7},
                'image-processing': {'check': 1, 'imag': 6},
                'mime-types': {'check': 4, 'imag': 9},
                'php': {'check': 0, 'imag': 5},
                'upload': {'check': 3, 'imag': 8}}





                share|improve this answer




























                  2












                  2








                  2







                  Use dictionary comprehension:



                  d = {k:v.reset_index(level=0, drop=True).to_dict() 
                  for k, v in df.groupby(level=0)['probability']}


                  Another solution:



                  d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}




                  print (d)

                  {'file-upload': {'check': 2, 'imag': 7},
                  'image-processing': {'check': 1, 'imag': 6},
                  'mime-types': {'check': 4, 'imag': 9},
                  'php': {'check': 0, 'imag': 5},
                  'upload': {'check': 3, 'imag': 8}}





                  share|improve this answer















                  Use dictionary comprehension:



                  d = {k:v.reset_index(level=0, drop=True).to_dict() 
                  for k, v in df.groupby(level=0)['probability']}


                  Another solution:



                  d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}




                  print (d)

                  {'file-upload': {'check': 2, 'imag': 7},
                  'image-processing': {'check': 1, 'imag': 6},
                  'mime-types': {'check': 4, 'imag': 9},
                  'php': {'check': 0, 'imag': 5},
                  'upload': {'check': 3, 'imag': 8}}






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 '18 at 11:25

























                  answered Nov 20 '18 at 11:14









                  jezraeljezrael

                  340k25294365




                  340k25294365

























                      0














                      I think this is the fastest method:



                      Your example:



                      df.reset_index(level=1, inplace=True)
                      df_dict = df.to_dict('your_index')


                      Simple example:



                      import pandas as pd
                      df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
                      df2.to_dict('id')


                      pandas.DataFrame.to_dict






                      share|improve this answer






























                        0














                        I think this is the fastest method:



                        Your example:



                        df.reset_index(level=1, inplace=True)
                        df_dict = df.to_dict('your_index')


                        Simple example:



                        import pandas as pd
                        df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
                        df2.to_dict('id')


                        pandas.DataFrame.to_dict






                        share|improve this answer




























                          0












                          0








                          0







                          I think this is the fastest method:



                          Your example:



                          df.reset_index(level=1, inplace=True)
                          df_dict = df.to_dict('your_index')


                          Simple example:



                          import pandas as pd
                          df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
                          df2.to_dict('id')


                          pandas.DataFrame.to_dict






                          share|improve this answer















                          I think this is the fastest method:



                          Your example:



                          df.reset_index(level=1, inplace=True)
                          df_dict = df.to_dict('your_index')


                          Simple example:



                          import pandas as pd
                          df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
                          df2.to_dict('id')


                          pandas.DataFrame.to_dict







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 20 '18 at 11:38

























                          answered Nov 20 '18 at 11:12









                          Rudolf MorkovskyiRudolf Morkovskyi

                          730117




                          730117






























                              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%2f53391438%2fcreate-a-dict-of-dict-with-2-columns-and-with-values-from-a-third-column-in-pand%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

                              Guess what letter conforming each word

                              Port of Spain

                              Run scheduled task as local user group (not BUILTIN)