1

Let´s say I have the following table: enter image description here Produced by the following python Code

import pandas as pd

data = [["Car","Sport","Wheel", 4], 
        ["Car", "Sport","engine HP", 65], 
        ["Car", "Sport","windows", 5], 
        ["Car","Van","Wheel", 4], 
        ["Car", "Van","engine HP", 85], 
        ["Car", "Van","windows", 8],
        ["Truck","Small","Wheel", 4], 
        ["Truck", "Small","engine HP", 125], 
        ["Truck", "Small","windows", 2],
        ["Truck","Large","Wheel", 8], 
        ["Truck", "Large","engine HP", 200], 
        ["Truck", "Large","windows", 2]
        ]
  
df = pd.DataFrame(data)

#define header names
df.columns = ["Vehicle", "Type","Parameter","Value"]

df here

How do I manipulate by Dataframe to transpose the parameter value if , I don't know in advance the content of the parameter columns, or how many type of parameters there might be.

The end result would be the following table

"Vehicle","Type","Wheel","Engine","Windows"

"Car","Sport",4,65,51

"Car","Van",4,85,8

"Truck","Small",4,125,2
"Truck","Large",8,200,2
sammywemmy
  • 27,093
  • 4
  • 17
  • 31
Julien
  • 1,028
  • 9
  • 18
  • why don't you use the built-in method of transpose provided by pandas? – Tron Nov 27 '22 at 21:59
  • It isn't clear to me what you want to do. In addition to showing your original data, can you show the result that you desire? If you want to do a trivial transpose operation, then just call `df.transpose()`. I'm guessing that because you talk about needing to know details of the data, you want to do something more complex. – CryptoFool Nov 27 '22 at 22:02
  • @CryptoFool yes, the table at the end is the result I want to get – Julien Nov 27 '22 at 22:06
  • 1
    use the following code: df.pivot(index=["Vehicle", "Type"], columns="Parameter", values="Value") – Sunilkumar Raghuraman Nov 27 '22 at 22:47

1 Answers1

0

You can try the built-in transpose method provided by pandas.

You can have a look about pandas.DataFrame.transpose

Tron
  • 566
  • 6
  • 7
  • Thank you, but The Transpose method would apply to the whole table and not produce the output table at the end of the question – Julien Nov 27 '22 at 22:07