Transposing a dataframe maintaining the first column as heading
Here is one way tmydf = setNames(data.frame(t(mydf[,-1])), mydf[,1])
Here is one way tmydf = setNames(data.frame(t(mydf[,-1])), mydf[,1])
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
My personal favourite is this gist: function transpose(a) { return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); }); }
$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);
You can use the PIVOT function to convert your rows of data into columns. Your original query can be used to retrieve all the data, the only change I would make to it would be to exclude the column b.field_id because this will alter the final display of the result. If you have a known … Read more
I saw that all of the answers create a new resultant matrix. This is simple: matrix[i][j] = matrix[j][i]; However, you can also do this in-place, in case of square matrix. // Transpose, where m == n for (int i = 0; i < m; i++) { for (int j = i + 1; j < … Read more
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
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