How to sort a dataFrame in python pandas by two or more columns based on a list of values?

Viewed 38

Suppose I have a dataframe with columns b. and c., I want to sort the dataframe by column b. in ascending order, and by column c. based on the order of a list, how do I do this?

Original df:

b.  c.
A   BA
A   PY
B   CH
A   CH
B   BA
C   BA
B   PY
C   PY

sort ascending for the "b." column and for "c." cloumn sort based on this list:

["PY","BA","MA","CH"]

so result should look something like this:

b.  c.
A   PY
A   BA
A   CH
B   PY
B   BA
B   CH
C   PY
C   CH
0 Answers
Related