Transpose columns to rows
nadha_rao
Registered Posts: 1 ✭
Below is my input data and expected output, tried with Pivot recipe, it did not work. Any suggestions please?
My input data:
Location | 123 | 165 | 342 | 125 |
1 | 10 | 18 | 27 | 52 |
4 | 5 | 23 | 2 | 2 |
12 | 54 | 25 | 9 | 65 |
6 | 23 | 8 | 11 | 23 |
99 | 67 | 14 | 45 | 12 |
232 | 34 | 10 | 12 | 19 |
10 | 35 | 21 | 19 | 27 |
8 | 11 | 78 | 89 | 18 |
Expected output:
Location | Name | width |
1 | 123 | 10 |
1 | 165 | 18 |
1 | 342 | 27 |
1 | 125 | 52 |
4 | 123 | 5 |
4 | 165 | 23 |
4 | 342 | 2 |
4 | 125 | 2 |
12 | 123 | 54 |
12 | 165 | 25 |
12 | 342 | 9 |
12 | 125 | 65 |
Answers
-
Hi @nadha_rao
,You can use a Python recipe with the code below:
import dataiku import pandas as pd # Read recipe inputs input = dataiku.Dataset("input") df = input.get_dataframe() d={"Location":[],"Name":[],"Width":[]} for i in range(len(df)) : for (j, column) in enumerate(df): if j!=0: d["Location"].append(df.iloc[i,0]) d["Name"].append(column) d["Width"].append(df.iloc[i,j]) # Write recipe outputs output = dataiku.Dataset("output") output.write_with_schema(pd.DataFrame(d))