I have two lists (columns from two separate pandas dataframes) and want to find the intersection of both lists while preserving the order, or ordering based on a condition. Consider the following example:
x = ['0 MO', '1 YR', '10 YR', '15 YR', '2 YR', '20 YR', '3 MO', '3 YR',
'30 YR', '4 YR', '5 YR', '6 MO', '7 YR', '9 MO', 'Country']
y = ['Industry', '3 MO', '6 MO', '9 MO', '1 YR', '2 YR', '3 YR',
'4 YR', '5 YR', '7 YR', '10 YR', '15 YR', '20 YR', '30 YR']
answer = set(x).intersection(y)
The variable answer yields the overlapping columns, yet the order is not preserved. Is there a way of sorting the solution such that the answer yields:
answer = ['3 MO', '6 MO', '9 MO', '1 YR', '2 YR', '3 YR',
'4 YR', '5 YR', '7 YR', '10 YR', '15 YR', '20 YR',
'30 YR']
i.e first sorting the intersected list by month ("MO") and integers, and then by year ("YR") and its integers?
Alternatively, is there a pandas method to obtain the same result with two dataframes of overlapping columns (preserving or stating order)?