transpose
Numpy transpose of 1D array not giving expected result
NumPy’s transpose() effectively reverses the shape of an array. If the array is one-dimensional, this means it has no effect. In NumPy, the arrays array([1, 2, 3]) and array([1, 2, 3]) are actually the same – they only differ in whitespace. What you probably want are the corresponding two-dimensional arrays, for which transpose() would work … Read more
Transpose column to row with Spark
Spark >= 3.4 You can use built-in melt method. With Python: df.melt( ids=[“A”], values=[“col_1”, “col_2″], variableColumnName=”key”, valueColumnName=”val” ) with Scala df.melt(Array($”A”), Array($”col_1″, $”col_2″), “key”, “val”) Spark < 3.4 It is relatively simple to do with basic Spark SQL functions. Python from pyspark.sql.functions import array, col, explode, struct, lit df = sc.parallelize([(1, 0.0, 0.6), (1, 0.6, … Read more
How to pivot a dataframe in Pandas? [duplicate]
You can use pivot_table: pd.pivot_table(df, values=”Value”, index=[‘Country’,’Year’], columns=”Indicator”).reset_index() this outputs: Indicator Country Year 1 2 3 4 5 0 Angola 2005 6 13 10 11 5 1 Angola 2006 3 2 7 3 6
Transpose latest rows per user to columns
Use crosstab() from the tablefunc module. SELECT * FROM crosstab( $$SELECT user_id, user_name, rn, email_address FROM ( SELECT u.user_id, u.user_name, e.email_address , row_number() OVER (PARTITION BY u.user_id ORDER BY e.creation_date DESC NULLS LAST) AS rn FROM usr u LEFT JOIN email_tbl e USING (user_id) ) sub WHERE rn < 4 ORDER BY user_id $$ , … Read more
Is there a php function like python’s zip?
As long as all the arrays are the same length, you can use array_map with null as the first argument. array_map(null, $a, $b, $c, …); If some of the arrays are shorter, they will be padded with nulls to the length of the longest, unlike python where the returned result is the length of the … Read more
What is the fastest way to transpose a matrix in C++?
This is a good question. There are many reason you would want to actually transpose the matrix in memory rather than just swap coordinates, e.g. in matrix multiplication and Gaussian smearing. First let me list one of the functions I use for the transpose (EDIT: please see the end of my answer where I found … Read more