Chunk and transpose a flat array into rows with a specific number of columns

Use array_chunk() to break the array up in to $cols: $chunks = array_chunk($array, ceil(count($array) / $cols)); Which would give you: array(array(‘A’, ‘B’, ‘C’, ‘D’), array(‘E’, ‘F’, ‘G’)); Then combine the values from each array where the keys match. array_unshift($chunks, null); $chunks = call_user_func_array(‘array_map’, $chunks); // array(array(‘A’, ‘E’), array(‘B’, ‘F’), array(‘C’, ‘G’), array(‘D’, NULL)) Here’s an … Read more

Transpose and flatten two-dimensional indexed array where rows may not be of equal length

$data = array( 0 => array( 0 => ‘1’, 1 => ‘a’, 2 => ‘3’, 3 => ‘c’, ), 1 => array( 0 => ‘2’, 1 => ‘b’, ), ); $newArray = array(); $mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY); $mi->attachIterator(new ArrayIterator($data[0])); $mi->attachIterator(new ArrayIterator($data[1])); foreach($mi as $details) { $newArray = array_merge( $newArray, array_filter($details) ); } var_dump($newArray);

Convert column to row in Python Pandas

You need set_index with transpose by T: print (df.set_index(‘fruits’).T) fruits apples grapes figs numFruits 10 20 15 If need rename columns, it is a bit complicated: print (df.rename(columns={‘numFruits’:’Market 1 Order’}) .set_index(‘fruits’) .rename_axis(None).T) apples grapes figs Market 1 Order 10 20 15 Another faster solution is use numpy.ndarray.reshape: print (pd.DataFrame(df.numFruits.values.reshape(1,-1), index=[‘Market 1 Order’], columns=df.fruits.values)) apples grapes … Read more

Rotate – Transposing a List using LINQ C#

This is a simple and flexible solution, it will handle multiple inner lists with any number of dimensions. List<List<string>> PersonInfo = new List<List<string>>() { new List<string>() {“John”, “Peter”, “Watson”}, new List<string>() {“1000”, “1001”, “1002”} }; var result = PersonInfo .SelectMany(inner => inner.Select((item, index) => new { item, index })) .GroupBy(i => i.index, i => i.item) … Read more