numpy.column_stack () in Python

Arrays | column_stack | NumPy | Python Methods and Functions

numpy.column_stack() is used to stack numpy.column_stack() arrays as columns into two-dimensional array. It takes a sequence of one-dimensional arrays and stacks them in columns to create one two-dimensional array.  hstack arrays are laid out as-is, as in the hstack function.

Syntax: numpy.column_stack (tup )

Parameters:
tup: [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same first dimension.
Return: [stacked 2-D array] The stacked 2-D array of the input arrays.

Code # 1:

# Python program explaining
# column_stack () function

 

import numpy as geek

 
# input array

in_arr1 = geek.array (( 1 , 2 , 3 ))

print "1st Input array:" , in_arr1) 

 

in_arr2 = geek. array (( 4 , 5 , 6 ))

print ( "2nd Input array:" , in_arr2) 

 
# Stacking two arrays

out_arr = geek.column_stack ((in_arr1, in_arr2))

print ( "Output stacked array:" , out_arr)

Output :

 1st Input array: [1 2 3] 2nd Input array: [4 5 6 ] Output stacked array: [[1 4] [2 5] [3 6]] 

Code # 2:

# Python program explaining
# column_stack () function

  

import numpy as geek

 
# input array

in_arr1 = geek.array ([[[ 1 , 2 , 3 ], [ - 1 , - 2 , - 3 ]])

print ( "1st Input array:" , in_arr1) 

 

in_arr2 = geek.array ([[ 4 , 5 , 6 ], [ - 4 , - 5 , - 6 ]])

print ( "2nd Input array:" , in_arr2) 

 
# Stacking two arrays

out_arr = geek.column_stack ((in_arr1, in_arr2))

print ( "Output stacked array:" , out_arr)

Exit:

 1st Input array: [[1 2 3] [-1 -2 -3]] 2nd Input array: [[4 5 6] [-4 -5 -6]] Output stacked array: [[1 2 3 4 5 6] [-1 -2 -3 -4 -5 -6]] 




numpy.column_stack () in Python: StackOverflow Questions

Answer #1

I think you"re almost there, try removing the extra square brackets around the lst"s (Also you don"t need to specify the column names when you"re creating a dataframe from a dict like this):

import pandas as pd
lst1 = range(100)
lst2 = range(100)
lst3 = range(100)
percentile_list = pd.DataFrame(
    {"lst1Title": lst1,
     "lst2Title": lst2,
     "lst3Title": lst3
    })

percentile_list
    lst1Title  lst2Title  lst3Title
0          0         0         0
1          1         1         1
2          2         2         2
3          3         3         3
4          4         4         4
5          5         5         5
6          6         6         6
...

If you need a more performant solution you can use np.column_stack rather than zip as in your first attempt, this has around a 2x speedup on the example here, however comes at bit of a cost of readability in my opinion:

import numpy as np
percentile_list = pd.DataFrame(np.column_stack([lst1, lst2, lst3]), 
                               columns=["lst1Title", "lst2Title", "lst3Title"])

Answer #2

In general you can concatenate a whole sequence of arrays along any axis:

numpy.concatenate( LIST, axis=0 )

but you do have to worry about the shape and dimensionality of each array in the list (for a 2-dimensional 3x5 output, you need to ensure that they are all 2-dimensional n-by-5 arrays already). If you want to concatenate 1-dimensional arrays as the rows of a 2-dimensional output, you need to expand their dimensionality.

As Jorge"s answer points out, there is also the function stack, introduced in numpy 1.10:

numpy.stack( LIST, axis=0 )

This takes the complementary approach: it creates a new view of each input array and adds an extra dimension (in this case, on the left, so each n-element 1D array becomes a 1-by-n 2D array) before concatenating. It will only work if all the input arrays have the same shape—even along the axis of concatenation.

vstack (or equivalently row_stack) is often an easier-to-use solution because it will take a sequence of 1- and/or 2-dimensional arrays and expand the dimensionality automatically where necessary and only where necessary, before concatenating the whole list together. Where a new dimension is required, it is added on the left. Again, you can concatenate a whole list at once without needing to iterate:

numpy.vstack( LIST )

This flexible behavior is also exhibited by the syntactic shortcut numpy.r_[ array1, ...., arrayN ] (note the square brackets). This is good for concatenating a few explicitly-named arrays but is no good for your situation because this syntax will not accept a sequence of arrays, like your LIST.

There is also an analogous function column_stack and shortcut c_[...], for horizontal (column-wise) stacking, as well as an almost-analogous function hstack—although for some reason the latter is less flexible (it is stricter about input arrays" dimensionality, and tries to concatenate 1-D arrays end-to-end instead of treating them as columns).

Finally, in the specific case of vertical stacking of 1-D arrays, the following also works:

numpy.array( LIST )

...because arrays can be constructed out of a sequence of other arrays, adding a new dimension to the beginning.

Get Solution for free from DataCamp guru