Transpose columns to rows

nadha_rao
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:

Location123165342125
110182752
452322
125425965
62381123
9967144512
23234101219
1035211927
811788918

Expected output:

LocationNamewidth
112310
116518
134227
112552
41235
416523
43422
41252
1212354
1216525
123429
1212565

Answers

  • Catalina
    Catalina Dataiker, Dataiku DSS Core Designer, Registered Posts: 135 Dataiker
    edited July 17

    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))

Setup Info
    Tags
      Help me…