I wanted to sort the dataframe based on the list. Dataframe consists of unique id's and I have a list of ids.
Note:- list not have all id's value. I used df.loc but it has limitations.
Example code is as follows:
import pandas as pd
ratings_dict = {
"ID": ["101", "102", "103", "104", "105"],
"title": ['TV', 'AC', 'Monitor', 'Headphone', 'Laptop'],
"rating": [1, 2, 2, 3, 2]
}
df = pd.DataFrame(ratings_dict)
trend_sort=["103","101"]
trend_sort is the id list.
df.set_index('ID',inplace=True)
df=df.loc[trend_sort]
After using df.loc I got output as,
Expected output:

