Nesting/grouping a range of columns when converting a Pandas DataFrame to a dictionaryConvert two lists into a dictionary in PythonConvert a String representation of a Dictionary to a dictionary?Selecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersConvert list of dictionaries to a pandas DataFrame

How to make all magic-casting innate, but still rare?

Lead the way to this Literary Knight to its final “DESTINATION”

How would Japanese people react to someone refusing to say “itadakimasu” for religious reasons?

New Site Design!

At what temperature should the earth be cooked to prevent human infection?

Is it a bad idea to have a pen name with only an initial for a surname?

What is the precise meaning of "подсел на мак"?

Is there any effect in D&D 5e that cannot be undone?

Is this set open or closed (or both?)

What do I put on my resume to make the company i'm applying to think i'm mature enough to handle a job?

What are the mechanical differences between Adapt and Monstrosity?

What is the context for Napoleon's quote "[the Austrians] did not know the value of five minutes"?

Having some issue with notation in a Hilbert space

Someone who is granted access to information but not expected to read it

Explicit direct #include vs. Non-contractual transitive #include

Catching a robber on one line

How can this shape perfectly cover a cube?

Redirecting output only on a successful command call

How do credit card companies know what type of business I'm paying for?

The instant an accelerating object has zero speed, is it speeding up, slowing down, or neither?

Is the infant mortality rate among African-American babies in Youngstown, Ohio greater than that of babies in Iran?

Using roof rails to set up hammock

Should I email my professor to clear up a (possibly very irrelevant) awkward misunderstanding?

Why are almost all the people in this orchestra recording wearing headphones with one ear on and one ear off?



Nesting/grouping a range of columns when converting a Pandas DataFrame to a dictionary


Convert two lists into a dictionary in PythonConvert a String representation of a Dictionary to a dictionary?Selecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headersConvert list of dictionaries to a pandas DataFrame






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








1















I've been trying to work out how to convert a Pandas DataFrame into a list of nested dictionaries and I haven't been having any luck.



My first thought was to convert the DataFrame into a list of dictionaries (with users = users.to_dict(orient='records')) and then merge the 'address' and 'color_preference' items into sublists but there must be a better way to do it!



I have a dataframe like this:



import pandas as pd
users = pd.DataFrame('email_address': ["email@email.com"], 'status': ["active"], 'address': ["1 Eagle St"], 'suburb': ["BROOKLYN"], 'state': ["NY"], 'postcode': ["11201"], 'country': ["USA"], 'red': [False], 'orange': [True], 'yellow': [True], 'green': [True], 'blue': [False], 'indigo': [False], 'violet': [False])


and I'm trying to convert it into this format:



 
"email_address":"email@email.com",
"status":"active",
"address":
"address":"1 Eagle St",
"suburb":"Brooklyn",
"state":"NY",
"postcode":"11201",
"country":"USA"
,
"color_preference":
"red":false,
"orange":true,
"yellow":true,
"green":true,
"blue":false,
"indigo":false,
"violet":false











share|improve this question




























    1















    I've been trying to work out how to convert a Pandas DataFrame into a list of nested dictionaries and I haven't been having any luck.



    My first thought was to convert the DataFrame into a list of dictionaries (with users = users.to_dict(orient='records')) and then merge the 'address' and 'color_preference' items into sublists but there must be a better way to do it!



    I have a dataframe like this:



    import pandas as pd
    users = pd.DataFrame('email_address': ["email@email.com"], 'status': ["active"], 'address': ["1 Eagle St"], 'suburb': ["BROOKLYN"], 'state': ["NY"], 'postcode': ["11201"], 'country': ["USA"], 'red': [False], 'orange': [True], 'yellow': [True], 'green': [True], 'blue': [False], 'indigo': [False], 'violet': [False])


    and I'm trying to convert it into this format:



     
    "email_address":"email@email.com",
    "status":"active",
    "address":
    "address":"1 Eagle St",
    "suburb":"Brooklyn",
    "state":"NY",
    "postcode":"11201",
    "country":"USA"
    ,
    "color_preference":
    "red":false,
    "orange":true,
    "yellow":true,
    "green":true,
    "blue":false,
    "indigo":false,
    "violet":false











    share|improve this question
























      1












      1








      1








      I've been trying to work out how to convert a Pandas DataFrame into a list of nested dictionaries and I haven't been having any luck.



      My first thought was to convert the DataFrame into a list of dictionaries (with users = users.to_dict(orient='records')) and then merge the 'address' and 'color_preference' items into sublists but there must be a better way to do it!



      I have a dataframe like this:



      import pandas as pd
      users = pd.DataFrame('email_address': ["email@email.com"], 'status': ["active"], 'address': ["1 Eagle St"], 'suburb': ["BROOKLYN"], 'state': ["NY"], 'postcode': ["11201"], 'country': ["USA"], 'red': [False], 'orange': [True], 'yellow': [True], 'green': [True], 'blue': [False], 'indigo': [False], 'violet': [False])


      and I'm trying to convert it into this format:



       
      "email_address":"email@email.com",
      "status":"active",
      "address":
      "address":"1 Eagle St",
      "suburb":"Brooklyn",
      "state":"NY",
      "postcode":"11201",
      "country":"USA"
      ,
      "color_preference":
      "red":false,
      "orange":true,
      "yellow":true,
      "green":true,
      "blue":false,
      "indigo":false,
      "violet":false











      share|improve this question














      I've been trying to work out how to convert a Pandas DataFrame into a list of nested dictionaries and I haven't been having any luck.



      My first thought was to convert the DataFrame into a list of dictionaries (with users = users.to_dict(orient='records')) and then merge the 'address' and 'color_preference' items into sublists but there must be a better way to do it!



      I have a dataframe like this:



      import pandas as pd
      users = pd.DataFrame('email_address': ["email@email.com"], 'status': ["active"], 'address': ["1 Eagle St"], 'suburb': ["BROOKLYN"], 'state': ["NY"], 'postcode': ["11201"], 'country': ["USA"], 'red': [False], 'orange': [True], 'yellow': [True], 'green': [True], 'blue': [False], 'indigo': [False], 'violet': [False])


      and I'm trying to convert it into this format:



       
      "email_address":"email@email.com",
      "status":"active",
      "address":
      "address":"1 Eagle St",
      "suburb":"Brooklyn",
      "state":"NY",
      "postcode":"11201",
      "country":"USA"
      ,
      "color_preference":
      "red":false,
      "orange":true,
      "yellow":true,
      "green":true,
      "blue":false,
      "indigo":false,
      "violet":false








      json python-3.x pandas dictionary






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 3:54









      jalexbinjalexbin

      173




      173






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You can do this explicitly with apply (I've done the first couple but you could do all the address/colors):



          def extract_json(row):
          return
          "email_address": row.loc["email_address"],
          "status": row.loc["status"],
          "address": row.loc[["address", "suburb"]].to_dict(),
          "color_preference": row.loc[["red", "orange"]].to_dict()


          In [11]: users.apply(extract_json, axis=1)
          Out[11]:
          0 {'email_address': 'email@email.com', 'status':...
          dtype: object

          In [12]: users.apply(extract_json, axis=1).tolist()
          Out[12]:
          ['email_address': 'email@email.com',
          'status': 'active',
          'address': 'address': '1 Eagle St', 'suburb': 'BROOKLYN',
          'color_preference': 'red': False, 'orange': True]


          You could pull out all the address/colors by position:



          In [21]: users.columns[2:7]
          Out[21]: Index(['address', 'suburb', 'state', 'postcode', 'country'], dtype='object')

          In [22]: users.columns[7:]
          Out[22]: Index(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'], dtype='object')





          share|improve this answer























          • I knew there had to be an easier way - thanks!

            – jalexbin
            Mar 25 at 6:42












          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%2f55331059%2fnesting-grouping-a-range-of-columns-when-converting-a-pandas-dataframe-to-a-dict%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 do this explicitly with apply (I've done the first couple but you could do all the address/colors):



          def extract_json(row):
          return
          "email_address": row.loc["email_address"],
          "status": row.loc["status"],
          "address": row.loc[["address", "suburb"]].to_dict(),
          "color_preference": row.loc[["red", "orange"]].to_dict()


          In [11]: users.apply(extract_json, axis=1)
          Out[11]:
          0 {'email_address': 'email@email.com', 'status':...
          dtype: object

          In [12]: users.apply(extract_json, axis=1).tolist()
          Out[12]:
          ['email_address': 'email@email.com',
          'status': 'active',
          'address': 'address': '1 Eagle St', 'suburb': 'BROOKLYN',
          'color_preference': 'red': False, 'orange': True]


          You could pull out all the address/colors by position:



          In [21]: users.columns[2:7]
          Out[21]: Index(['address', 'suburb', 'state', 'postcode', 'country'], dtype='object')

          In [22]: users.columns[7:]
          Out[22]: Index(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'], dtype='object')





          share|improve this answer























          • I knew there had to be an easier way - thanks!

            – jalexbin
            Mar 25 at 6:42
















          1














          You can do this explicitly with apply (I've done the first couple but you could do all the address/colors):



          def extract_json(row):
          return
          "email_address": row.loc["email_address"],
          "status": row.loc["status"],
          "address": row.loc[["address", "suburb"]].to_dict(),
          "color_preference": row.loc[["red", "orange"]].to_dict()


          In [11]: users.apply(extract_json, axis=1)
          Out[11]:
          0 {'email_address': 'email@email.com', 'status':...
          dtype: object

          In [12]: users.apply(extract_json, axis=1).tolist()
          Out[12]:
          ['email_address': 'email@email.com',
          'status': 'active',
          'address': 'address': '1 Eagle St', 'suburb': 'BROOKLYN',
          'color_preference': 'red': False, 'orange': True]


          You could pull out all the address/colors by position:



          In [21]: users.columns[2:7]
          Out[21]: Index(['address', 'suburb', 'state', 'postcode', 'country'], dtype='object')

          In [22]: users.columns[7:]
          Out[22]: Index(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'], dtype='object')





          share|improve this answer























          • I knew there had to be an easier way - thanks!

            – jalexbin
            Mar 25 at 6:42














          1












          1








          1







          You can do this explicitly with apply (I've done the first couple but you could do all the address/colors):



          def extract_json(row):
          return
          "email_address": row.loc["email_address"],
          "status": row.loc["status"],
          "address": row.loc[["address", "suburb"]].to_dict(),
          "color_preference": row.loc[["red", "orange"]].to_dict()


          In [11]: users.apply(extract_json, axis=1)
          Out[11]:
          0 {'email_address': 'email@email.com', 'status':...
          dtype: object

          In [12]: users.apply(extract_json, axis=1).tolist()
          Out[12]:
          ['email_address': 'email@email.com',
          'status': 'active',
          'address': 'address': '1 Eagle St', 'suburb': 'BROOKLYN',
          'color_preference': 'red': False, 'orange': True]


          You could pull out all the address/colors by position:



          In [21]: users.columns[2:7]
          Out[21]: Index(['address', 'suburb', 'state', 'postcode', 'country'], dtype='object')

          In [22]: users.columns[7:]
          Out[22]: Index(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'], dtype='object')





          share|improve this answer













          You can do this explicitly with apply (I've done the first couple but you could do all the address/colors):



          def extract_json(row):
          return
          "email_address": row.loc["email_address"],
          "status": row.loc["status"],
          "address": row.loc[["address", "suburb"]].to_dict(),
          "color_preference": row.loc[["red", "orange"]].to_dict()


          In [11]: users.apply(extract_json, axis=1)
          Out[11]:
          0 {'email_address': 'email@email.com', 'status':...
          dtype: object

          In [12]: users.apply(extract_json, axis=1).tolist()
          Out[12]:
          ['email_address': 'email@email.com',
          'status': 'active',
          'address': 'address': '1 Eagle St', 'suburb': 'BROOKLYN',
          'color_preference': 'red': False, 'orange': True]


          You could pull out all the address/colors by position:



          In [21]: users.columns[2:7]
          Out[21]: Index(['address', 'suburb', 'state', 'postcode', 'country'], dtype='object')

          In [22]: users.columns[7:]
          Out[22]: Index(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'], dtype='object')






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 4:26









          Andy HaydenAndy Hayden

          201k56451443




          201k56451443












          • I knew there had to be an easier way - thanks!

            – jalexbin
            Mar 25 at 6:42


















          • I knew there had to be an easier way - thanks!

            – jalexbin
            Mar 25 at 6:42

















          I knew there had to be an easier way - thanks!

          – jalexbin
          Mar 25 at 6:42






          I knew there had to be an easier way - thanks!

          – jalexbin
          Mar 25 at 6:42


















          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%2f55331059%2fnesting-grouping-a-range-of-columns-when-converting-a-pandas-dataframe-to-a-dict%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