flatten

Turn Pandas Multi-Index into column

Turn Pandas Multi-Index into column Question: I have a dataframe with 2 index levels: value Trial measurement 1 0 13 1 3 2 4 2 0 NaN 1 12 3 0 34 Which I want to turn into this: Trial measurement value 1 0 13 1 1 3 1 2 4 2 0 NaN 2 …

Total answers: 8

How to flatten only some dimensions of a numpy array

How to flatten only some dimensions of a numpy array Question: Is there a quick way to “sub-flatten” or flatten only some of the first dimensions in a numpy array? For example, given a numpy array of dimensions (50,100,25), the resultant dimensions would be (5000,25) Asked By: IssamLaradji || Source Answers: Take a look at …

Total answers: 5

How to flatten a tuple in python

How to flatten a tuple in python Question: I have the following element of a list, and the list is 100 elements long. [(50, (2.7387451803816479e-13, 219))] How do I convert each element to look like this? [(50, 2.7387451803816479e-13, 219)] Asked By: olliepower || Source Answers: [(a, b, c) for a, (b, c) in l] Tuple …

Total answers: 6

Concatenation of inner lists or ints

Concatenation of inner lists or ints Question: I feel like I’m missing something obvious, but there it is… I would like to go from: lst = [[0, 1, 3, 7, 8, 11, 12], [8, 0, 1, 2, 3, 14], 2] to: output = [0, 1, 3, 7, 8, 11, 12, 8, 0, 1, 2, 3, …

Total answers: 3

Python list comprehension, unpacking and multiple operations

Python list comprehension, unpacking and multiple operations Question: I want to unpack the tuples I create by doing the following so he the result is just one simple list. I can get the desired result in 2-3 lines but surely there is a oneliner list.comp? >>> x = range(10) >>> y = [(i,j**2) for i,j …

Total answers: 2

Flatten an irregular list of lists

Flatten an irregular (arbitrarily nested) list of lists Question: Yes, I know this subject has been covered before: Python idiom to chain (flatten) an infinite iterable of finite iterables? Flattening a shallow list in Python Comprehension for flattening a sequence of sequences? How do I make a flat list out of a list of lists? …

Total answers: 51

How do I make a flat list out of a list of lists?

How do I make a flat list out of a list of lists? Question: I have a list of lists like [[1, 2, 3], [4, 5, 6], [7], [8, 9]]. How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]? If your list of lists comes from a nested …

Total answers: 36