Python dictionary of list to Pandas dataframe












1















I am trying to transform a dictionary of lists (looks like a dictionary of dictionary, but is unfortunately a dictionary of lists) into a dataframe. I want to have the column-names from the list objects. So far i found a way to to turn the dictionary into a data frame, but the columns don't have the appropriate name and the values still contain the column names.



user_dict = {'Category 1': ['att_1: 1', 'att_2:  whatever'],
'Category 2': ['att_1 : 23', 'att_2 : another']}

res = pd.DataFrame.from_dict(user_dict, orient='index')
res.columns = [f'SYN{i+1}' for i in res]


Example Output:



                att_1 | att_2 

Category_1 1 | whatever

Category_1 23 | another


I was thinking at using unlist or regex, but I am not sure where to input that. Any help much appreciated! Thank you



Edit:
my unlist attemp ended here:



pd.DataFrame.from_dict({(i,j): to_dict(unlist(user_dict[i][j])) 
for i in user_dict.keys()
for j in user_dict[i].keys()},
orient='index')









share|improve this question





























    1















    I am trying to transform a dictionary of lists (looks like a dictionary of dictionary, but is unfortunately a dictionary of lists) into a dataframe. I want to have the column-names from the list objects. So far i found a way to to turn the dictionary into a data frame, but the columns don't have the appropriate name and the values still contain the column names.



    user_dict = {'Category 1': ['att_1: 1', 'att_2:  whatever'],
    'Category 2': ['att_1 : 23', 'att_2 : another']}

    res = pd.DataFrame.from_dict(user_dict, orient='index')
    res.columns = [f'SYN{i+1}' for i in res]


    Example Output:



                    att_1 | att_2 

    Category_1 1 | whatever

    Category_1 23 | another


    I was thinking at using unlist or regex, but I am not sure where to input that. Any help much appreciated! Thank you



    Edit:
    my unlist attemp ended here:



    pd.DataFrame.from_dict({(i,j): to_dict(unlist(user_dict[i][j])) 
    for i in user_dict.keys()
    for j in user_dict[i].keys()},
    orient='index')









    share|improve this question



























      1












      1








      1








      I am trying to transform a dictionary of lists (looks like a dictionary of dictionary, but is unfortunately a dictionary of lists) into a dataframe. I want to have the column-names from the list objects. So far i found a way to to turn the dictionary into a data frame, but the columns don't have the appropriate name and the values still contain the column names.



      user_dict = {'Category 1': ['att_1: 1', 'att_2:  whatever'],
      'Category 2': ['att_1 : 23', 'att_2 : another']}

      res = pd.DataFrame.from_dict(user_dict, orient='index')
      res.columns = [f'SYN{i+1}' for i in res]


      Example Output:



                      att_1 | att_2 

      Category_1 1 | whatever

      Category_1 23 | another


      I was thinking at using unlist or regex, but I am not sure where to input that. Any help much appreciated! Thank you



      Edit:
      my unlist attemp ended here:



      pd.DataFrame.from_dict({(i,j): to_dict(unlist(user_dict[i][j])) 
      for i in user_dict.keys()
      for j in user_dict[i].keys()},
      orient='index')









      share|improve this question
















      I am trying to transform a dictionary of lists (looks like a dictionary of dictionary, but is unfortunately a dictionary of lists) into a dataframe. I want to have the column-names from the list objects. So far i found a way to to turn the dictionary into a data frame, but the columns don't have the appropriate name and the values still contain the column names.



      user_dict = {'Category 1': ['att_1: 1', 'att_2:  whatever'],
      'Category 2': ['att_1 : 23', 'att_2 : another']}

      res = pd.DataFrame.from_dict(user_dict, orient='index')
      res.columns = [f'SYN{i+1}' for i in res]


      Example Output:



                      att_1 | att_2 

      Category_1 1 | whatever

      Category_1 23 | another


      I was thinking at using unlist or regex, but I am not sure where to input that. Any help much appreciated! Thank you



      Edit:
      my unlist attemp ended here:



      pd.DataFrame.from_dict({(i,j): to_dict(unlist(user_dict[i][j])) 
      for i in user_dict.keys()
      for j in user_dict[i].keys()},
      orient='index')






      python python-3.x pandas list dictionary






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 10:15









      jpp

      102k2165116




      102k2165116










      asked Nov 16 '18 at 9:54









      SimonSimon

      369




      369
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can use a dictionary comprehension to restructure your input into a dictionary of dictionaries. Then use from_dict with orient='index':



          user_dict = {'Category 1': ['att_1: 1', 'att_2:  whatever'],
          'Category 2': ['att_1 : 23', 'att_2 : another']}

          d = {k: dict(map(str.strip, x.split(':')) for x in v) for k, v in user_dict.items()}

          df = pd.DataFrame.from_dict(d, orient='index')

          df['att_1'] = pd.to_numeric(df['att_1'])

          print(df)

          att_1 att_2
          Category 1 1 whatever
          Category 2 23 another


          As above, you will need to then convert series to numeric as appropriate.






          share|improve this answer
























          • This works very well for the proposed input. Thank you! (edit: silly follow up question removed)

            – Simon
            Nov 16 '18 at 10:27














          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%2f53335319%2fpython-dictionary-of-list-to-pandas-dataframe%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









          1














          You can use a dictionary comprehension to restructure your input into a dictionary of dictionaries. Then use from_dict with orient='index':



          user_dict = {'Category 1': ['att_1: 1', 'att_2:  whatever'],
          'Category 2': ['att_1 : 23', 'att_2 : another']}

          d = {k: dict(map(str.strip, x.split(':')) for x in v) for k, v in user_dict.items()}

          df = pd.DataFrame.from_dict(d, orient='index')

          df['att_1'] = pd.to_numeric(df['att_1'])

          print(df)

          att_1 att_2
          Category 1 1 whatever
          Category 2 23 another


          As above, you will need to then convert series to numeric as appropriate.






          share|improve this answer
























          • This works very well for the proposed input. Thank you! (edit: silly follow up question removed)

            – Simon
            Nov 16 '18 at 10:27


















          1














          You can use a dictionary comprehension to restructure your input into a dictionary of dictionaries. Then use from_dict with orient='index':



          user_dict = {'Category 1': ['att_1: 1', 'att_2:  whatever'],
          'Category 2': ['att_1 : 23', 'att_2 : another']}

          d = {k: dict(map(str.strip, x.split(':')) for x in v) for k, v in user_dict.items()}

          df = pd.DataFrame.from_dict(d, orient='index')

          df['att_1'] = pd.to_numeric(df['att_1'])

          print(df)

          att_1 att_2
          Category 1 1 whatever
          Category 2 23 another


          As above, you will need to then convert series to numeric as appropriate.






          share|improve this answer
























          • This works very well for the proposed input. Thank you! (edit: silly follow up question removed)

            – Simon
            Nov 16 '18 at 10:27
















          1












          1








          1







          You can use a dictionary comprehension to restructure your input into a dictionary of dictionaries. Then use from_dict with orient='index':



          user_dict = {'Category 1': ['att_1: 1', 'att_2:  whatever'],
          'Category 2': ['att_1 : 23', 'att_2 : another']}

          d = {k: dict(map(str.strip, x.split(':')) for x in v) for k, v in user_dict.items()}

          df = pd.DataFrame.from_dict(d, orient='index')

          df['att_1'] = pd.to_numeric(df['att_1'])

          print(df)

          att_1 att_2
          Category 1 1 whatever
          Category 2 23 another


          As above, you will need to then convert series to numeric as appropriate.






          share|improve this answer













          You can use a dictionary comprehension to restructure your input into a dictionary of dictionaries. Then use from_dict with orient='index':



          user_dict = {'Category 1': ['att_1: 1', 'att_2:  whatever'],
          'Category 2': ['att_1 : 23', 'att_2 : another']}

          d = {k: dict(map(str.strip, x.split(':')) for x in v) for k, v in user_dict.items()}

          df = pd.DataFrame.from_dict(d, orient='index')

          df['att_1'] = pd.to_numeric(df['att_1'])

          print(df)

          att_1 att_2
          Category 1 1 whatever
          Category 2 23 another


          As above, you will need to then convert series to numeric as appropriate.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 10:13









          jppjpp

          102k2165116




          102k2165116













          • This works very well for the proposed input. Thank you! (edit: silly follow up question removed)

            – Simon
            Nov 16 '18 at 10:27





















          • This works very well for the proposed input. Thank you! (edit: silly follow up question removed)

            – Simon
            Nov 16 '18 at 10:27



















          This works very well for the proposed input. Thank you! (edit: silly follow up question removed)

          – Simon
          Nov 16 '18 at 10:27







          This works very well for the proposed input. Thank you! (edit: silly follow up question removed)

          – Simon
          Nov 16 '18 at 10:27






















          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%2f53335319%2fpython-dictionary-of-list-to-pandas-dataframe%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python