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;
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
add a comment |
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
add a comment |
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
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
python
asked Mar 26 at 18:42
VeddaVedda
2,5292 gold badges22 silver badges50 bronze badges
2,5292 gold badges22 silver badges50 bronze badges
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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)
add a comment |
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'])
add a comment |
you can use this code:
new_lst = []
for group in lst:
for n in group:
new_lst.append("group":lst.index(group),"value": n)
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
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
add a comment |
add a comment |
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'])
add a comment |
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'])
add a comment |
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'])
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'])
answered Mar 26 at 18:48
ameame
2762 silver badges5 bronze badges
2762 silver badges5 bronze badges
add a comment |
add a comment |
you can use this code:
new_lst = []
for group in lst:
for n in group:
new_lst.append("group":lst.index(group),"value": n)
add a comment |
you can use this code:
new_lst = []
for group in lst:
for n in group:
new_lst.append("group":lst.index(group),"value": n)
add a comment |
you can use this code:
new_lst = []
for group in lst:
for n in group:
new_lst.append("group":lst.index(group),"value": n)
you can use this code:
new_lst = []
for group in lst:
for n in group:
new_lst.append("group":lst.index(group),"value": n)
answered Mar 26 at 19:13
Ali HallajiAli Hallaji
1,00112 silver badges20 bronze badges
1,00112 silver badges20 bronze badges
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 26 at 18:47
Born Tbe WastedBorn Tbe Wasted
57313 bronze badges
57313 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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