1

This question is similar to this, the last time I thought it will be that simple but it seems not (thank you @anon01 and @Ch3steR who answered my previous question there)

so here is my new dataframe

matrix = [(1, 3, 2, "1a", "3a", "2a"),
          (6, 5, 4, "6a", "5a", "4a"),
          (8, 7, 9, "8a", "7a", "9a"),
          ]
df = pd.DataFrame(matrix, index=list('abc'), columns=["price1","price2","price3","product1","product2","product3"])

    price1  price2  price3  product1    product2    product3
a   1         3       2       1a           3a         2a
b   6         5       4       6a           5a         4a
c   8         7       9       8a           7a         9a

I need to sort by price within each row but price and product is a pairs so if the price move to price1 then the product also need to move to product1 because they are pairs Here is the output will be

        price1  price2  price3  product1    product2    product3
a         1       2        3       1a         2a          3a
b         4       5        6       4a         5a          6a
c         7       8        9       7a         8a          9a

from the last question, I tried the suggested solution using np.sort it can work to sort the price but if I have another column it is not working. I tried to rematching the product with the price but I think it will cost more so I still using my previous brute force solution as using swapping from this link

df.loc[df['price1']>df['price2'],['price1','price2','product1','product2']] = df.loc[df['price1']>df['price2'],['price2','price1','product2','product1']].values
df.loc[df['price1']>df['price3'],['price1','price3','product1','product3']] = df.loc[df['price1']>df['price3'],['price3','price1','product3','product1']].values
df.loc[df['price2']>df['price3'],['price2','price3','product2','product3']] = df.loc[df['price2']>df['price3'],['price3','price2','product3','product2']].values

The problem is I have more pairs than 3 if someone has an idea for this matter, it will be very helpful, thank you

1 Answer 1

4

We can make use of numpy.sort for "price", and numpy.argsort for product. This is all vectorized by numpy.

# Gets all "price" columns
price = df.filter(like='price')
# Gets all "product" columns
product = df.filter(like='product')

# Sorts "price" columns row-wise and assigns an array back
df[price.columns] = np.sort(price.to_numpy(), axis=1)

# Builds indices for re-organizing "product" based on sorted "price" 
ix = np.arange(product.shape[1])[:,None]
iy = np.argsort(price.to_numpy(), axis=1)
# Re-arranges "product" array and assigns it back  
df[product.columns] = product.to_numpy()[ix, iy]
df
   price1  price2  price3 product1 product2 product3
a       1       2       3       1a       2a       3a
b       4       5       6       4a       5a       6a
c       7       8       9       7a       8a       9a
5
  • Comments inlined.
    – cs95
    Dec 24, 2020 at 9:01
  • thank you it is works, I think I can use more columns with this
    – d_frEak
    Dec 24, 2020 at 9:02
  • @d_frEak Potentially as many as you want, as long as len(price.columns) == len(product.columns) is True. (You could potentially add an if check to guard against erroneous cases in the code if you want.)
    – cs95
    Dec 24, 2020 at 9:03
  • yeah. I also planning using loop for product, because actually the pairs is not only (price, product) but more, so I plan to using price to sort columns row-wise and re-organizing the rest of the column thank you
    – d_frEak
    Dec 24, 2020 at 9:11
  • 1
    @cs95 great answer. wish you a very happy Christmas :-) Dec 24, 2020 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.