user2458922
user2458922

Reputation: 1721

Python DeDuplicate Sort columns and transform to a row

Given Data

Order_1 | Order_2 | Order_3
-----------------------
Bread    Egg        Key
Egg      Pen        Tea
         Pen        Book
         Key        Bag

Needs to be transformed as

Order_1,    Egg,    Bread       
Order_2,    Egg,    Key,    Pen 
Order_3,    Bag,    Book,   Key,    Tea

The Header became the index. Each Columns is sorted and De Duplicated.

Upvotes: 0

Views: 31

Answers (1)

Toby Petty
Toby Petty

Reputation: 4660

You can remove duplicates and sort columns with this loop:

for c in df.columns:
    vals = sorted(df[c].dropna().unique())
    df[c] = vals + [np.nan for x in range(len(df) - len(vals))]

Then just transpose the DataFrame:

df = df.T

Upvotes: 1

Related Questions