I have a pandas DataFrame like below:
df = pd.DataFrame({["id": [1, 2, 3, 4, 5],
"fruit": ["apple", "banana", "pineapple", "orange", "orange"],
"trash": [38, 22, 93, 1, 15]})
Now I want to sort the rows of this DataFrame on column fruit
, according to a provided ordered list. Lets say this list is:
ordered_list = ["pinapple", "banana", "orange", "apple"]
I would like to produce the following output (I don't care about the order of rows with the same fruit value, so the orange
rows might be reversed):
id fruit trash
3 pineapple 93
2 banana 22
4 orange 1
5 orange 15
1 apple 38
How to solve this? I know I could get around with some for-loops, but it would surprise me if there isn't a more elegant solution (which I'm not able to find). The solution should be as time-efficient as possible, since the real DataFrame consists of about 5000 rows (with around 50 unique fruit
values)