Unique identifier for each list of list in data frameHow do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?Finding the index of an item given a list containing it in PythonWhat 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 to clone or copy a list?How do I list all files of a directory?Fastest way to check if a value exist in a listGet unique values from a list in python

Decreasing star size

Converting 8V AC to 8V DC - bridge rectifier gets very hot while idling

May a man marry the women with whom he committed adultery?

Is there an antonym for "spicy" or "hot" regarding food (NOT "seasoned" but "spicy")?

How did the Axis intend to hold the Caucasus?

Is this photo showing a woman standing in the nude before teenagers real?

Is a topological space considered to be a class in set theory?

Did the IBM PC use the 8088's NMI line?

The best place for swimming in Arctic Ocean

Writing a clean implementation of rock–paper–scissors game in C++

Why do planes need a roll motion?

Am I allowed to use personal conversation as a source?

Use cases for M-0 & C-0?

Please explain joy and/or the Kimatthiyasutta

Why isn't there a serious attempt at creating a third mass-appeal party in the US?

Word for showing a small part of something briefly to hint to its existence or beauty without fully uncovering it

How to apply the changes to my `.zshrc` file after edit?

Why can't my huge trees be chopped down?

Why is 'n' preferred over "n" for output streams?

If a 2019 UA artificer has the Repeating Shot infusion on two hand crossbows, can they use two-weapon fighting?

Melee or Ranged attacks by Monsters, no distinction in modifiers?

Assuring luggage isn't lost with short layover

How to judge a Ph.D. applicant that arrives "out of thin air"

(2 of 11: Moon-or-Sun) What is Pyramid Cult's Favorite Camera?



Unique identifier for each list of list in data frame


How do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?Finding the index of an item given a list containing it in PythonWhat 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 to clone or copy a list?How do I list all files of a directory?Fastest way to check if a value exist in a listGet unique values from a list in python






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I have a list of list,



lst = [[2, 0, 1, 6, 7, 8], [4, 3, 5]] 


and I want to flatten the list and assign a unique id to each list in the list merged into a data.frame.



Desired output:



 value group
0 2 0
1 0 0
2 1 0
3 6 0
4 7 0
5 8 0
6 4 1
7 3 1
8 5 1









share|improve this question




























    -1















    I have a list of list,



    lst = [[2, 0, 1, 6, 7, 8], [4, 3, 5]] 


    and I want to flatten the list and assign a unique id to each list in the list merged into a data.frame.



    Desired output:



     value group
    0 2 0
    1 0 0
    2 1 0
    3 6 0
    4 7 0
    5 8 0
    6 4 1
    7 3 1
    8 5 1









    share|improve this question
























      -1












      -1








      -1








      I have a list of list,



      lst = [[2, 0, 1, 6, 7, 8], [4, 3, 5]] 


      and I want to flatten the list and assign a unique id to each list in the list merged into a data.frame.



      Desired output:



       value group
      0 2 0
      1 0 0
      2 1 0
      3 6 0
      4 7 0
      5 8 0
      6 4 1
      7 3 1
      8 5 1









      share|improve this question














      I have a list of list,



      lst = [[2, 0, 1, 6, 7, 8], [4, 3, 5]] 


      and I want to flatten the list and assign a unique id to each list in the list merged into a data.frame.



      Desired output:



       value group
      0 2 0
      1 0 0
      2 1 0
      3 6 0
      4 7 0
      5 8 0
      6 4 1
      7 3 1
      8 5 1






      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 18:42









      VeddaVedda

      2,5292 gold badges22 silver badges50 bronze badges




      2,5292 gold badges22 silver badges50 bronze badges






















          4 Answers
          4






          active

          oldest

          votes


















          1














          If you want a Pandas DataFrame:



          import pandas as pd

          lst = [[2, 0, 1, 6, 7, 8], [4, 3, 5]]

          final_list = []
          for i, l in enumerate(lst):
          for num in l:
          final_list.append('value': num, 'group': i)

          df = pd.DataFrame(final_list)





          share|improve this answer
































            2














            You're going to need to do some fancy flattening:



            flattened = [(item, index) for index, sublist in enumerate(lst) for item in sublist]
            df = pd.DataFrame(flattened, columns=['value','group'])





            share|improve this answer






























              1














              you can use this code:



              new_lst = []
              for group in lst:
              for n in group:
              new_lst.append("group":lst.index(group),"value": n)





              share|improve this answer






























                0














                You should try something before asking for desired output.



                Looping through a list of list, whilst having a unique identifier, you may want to use the function enumerate that "gives the indexer" of the list.



                for i,sub_list in enumerate(lst):
                identifier = i
                [(value,identifier) for value in sublist]
                ....


                Hoping this will help






                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%2f55364202%2funique-identifier-for-each-list-of-list-in-data-frame%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  1














                  If you want a Pandas DataFrame:



                  import pandas as pd

                  lst = [[2, 0, 1, 6, 7, 8], [4, 3, 5]]

                  final_list = []
                  for i, l in enumerate(lst):
                  for num in l:
                  final_list.append('value': num, 'group': i)

                  df = pd.DataFrame(final_list)





                  share|improve this answer





























                    1














                    If you want a Pandas DataFrame:



                    import pandas as pd

                    lst = [[2, 0, 1, 6, 7, 8], [4, 3, 5]]

                    final_list = []
                    for i, l in enumerate(lst):
                    for num in l:
                    final_list.append('value': num, 'group': i)

                    df = pd.DataFrame(final_list)





                    share|improve this answer



























                      1












                      1








                      1







                      If you want a Pandas DataFrame:



                      import pandas as pd

                      lst = [[2, 0, 1, 6, 7, 8], [4, 3, 5]]

                      final_list = []
                      for i, l in enumerate(lst):
                      for num in l:
                      final_list.append('value': num, 'group': i)

                      df = pd.DataFrame(final_list)





                      share|improve this answer















                      If you want a Pandas DataFrame:



                      import pandas as pd

                      lst = [[2, 0, 1, 6, 7, 8], [4, 3, 5]]

                      final_list = []
                      for i, l in enumerate(lst):
                      for num in l:
                      final_list.append('value': num, 'group': i)

                      df = pd.DataFrame(final_list)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 26 at 23:47









                      Luke Ning

                      1176 bronze badges




                      1176 bronze badges










                      answered Mar 26 at 18:48









                      liamhawkinsliamhawkins

                      6761 gold badge6 silver badges23 bronze badges




                      6761 gold badge6 silver badges23 bronze badges























                          2














                          You're going to need to do some fancy flattening:



                          flattened = [(item, index) for index, sublist in enumerate(lst) for item in sublist]
                          df = pd.DataFrame(flattened, columns=['value','group'])





                          share|improve this answer



























                            2














                            You're going to need to do some fancy flattening:



                            flattened = [(item, index) for index, sublist in enumerate(lst) for item in sublist]
                            df = pd.DataFrame(flattened, columns=['value','group'])





                            share|improve this answer

























                              2












                              2








                              2







                              You're going to need to do some fancy flattening:



                              flattened = [(item, index) for index, sublist in enumerate(lst) for item in sublist]
                              df = pd.DataFrame(flattened, columns=['value','group'])





                              share|improve this answer













                              You're going to need to do some fancy flattening:



                              flattened = [(item, index) for index, sublist in enumerate(lst) for item in sublist]
                              df = pd.DataFrame(flattened, columns=['value','group'])






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 26 at 18:48









                              ameame

                              2762 silver badges5 bronze badges




                              2762 silver badges5 bronze badges





















                                  1














                                  you can use this code:



                                  new_lst = []
                                  for group in lst:
                                  for n in group:
                                  new_lst.append("group":lst.index(group),"value": n)





                                  share|improve this answer



























                                    1














                                    you can use this code:



                                    new_lst = []
                                    for group in lst:
                                    for n in group:
                                    new_lst.append("group":lst.index(group),"value": n)





                                    share|improve this answer

























                                      1












                                      1








                                      1







                                      you can use this code:



                                      new_lst = []
                                      for group in lst:
                                      for n in group:
                                      new_lst.append("group":lst.index(group),"value": n)





                                      share|improve this answer













                                      you can use this code:



                                      new_lst = []
                                      for group in lst:
                                      for n in group:
                                      new_lst.append("group":lst.index(group),"value": n)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 26 at 19:13









                                      Ali HallajiAli Hallaji

                                      1,00112 silver badges20 bronze badges




                                      1,00112 silver badges20 bronze badges





















                                          0














                                          You should try something before asking for desired output.



                                          Looping through a list of list, whilst having a unique identifier, you may want to use the function enumerate that "gives the indexer" of the list.



                                          for i,sub_list in enumerate(lst):
                                          identifier = i
                                          [(value,identifier) for value in sublist]
                                          ....


                                          Hoping this will help






                                          share|improve this answer



























                                            0














                                            You should try something before asking for desired output.



                                            Looping through a list of list, whilst having a unique identifier, you may want to use the function enumerate that "gives the indexer" of the list.



                                            for i,sub_list in enumerate(lst):
                                            identifier = i
                                            [(value,identifier) for value in sublist]
                                            ....


                                            Hoping this will help






                                            share|improve this answer

























                                              0












                                              0








                                              0







                                              You should try something before asking for desired output.



                                              Looping through a list of list, whilst having a unique identifier, you may want to use the function enumerate that "gives the indexer" of the list.



                                              for i,sub_list in enumerate(lst):
                                              identifier = i
                                              [(value,identifier) for value in sublist]
                                              ....


                                              Hoping this will help






                                              share|improve this answer













                                              You should try something before asking for desired output.



                                              Looping through a list of list, whilst having a unique identifier, you may want to use the function enumerate that "gives the indexer" of the list.



                                              for i,sub_list in enumerate(lst):
                                              identifier = i
                                              [(value,identifier) for value in sublist]
                                              ....


                                              Hoping this will help







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Mar 26 at 18:47









                                              Born Tbe WastedBorn Tbe Wasted

                                              57313 bronze badges




                                              57313 bronze badges



























                                                  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%2f55364202%2funique-identifier-for-each-list-of-list-in-data-frame%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