I posted this to stackoverflow but didn't get any answers. Here's a link to the question: https://stackoverflow.com/questions/58193178/how-to-find-values-in-row-in-pandas-after-filtering-out-certain-data-and-then
Here's the question:
I have a df with 8 currencies (CAD, AUD, NOK, SEK, NZD, EUR, GBP, JPY) and 2 different data points: 12M PR (12 month price return) and 1Y ZS (1 year z score).
df = {'Date': ('2015-10-6', '2015-10-7'),
'CAD 12M PR': (-7.4, -4.9),
'AUD 12M PR': (-2.3, -1.6),
'NOK 12M PR': (2.6, 6.4),
'SEK 12M PR': (6.7, 8.6),
'NZD 12M PR': (3.1, 2.9),
'EUR 12M PR': (2.2, 4.7),
'GBP 12M PR': (-3.6, -2.5),
'JPY 12M PR': (13.8, 15.7),
'CAD 12M ZS': (3.1, 2.5),
'AUD 12M ZS': (-1.7, 3.0),
'NOK 12M ZS': (2.0, 1.8),
'SEK 12M ZS': (2.6, 2.6),
'NZD 12M ZS': (-4.5, -5.6),
'EUR 12M ZS': (-3.8, -3.7),
'GBP 12M ZS': (-1.6, -2.7),
'JPY 12M ZS': (3.0, 2.1)
}
I want to first apply a filter by retrieving the lowest 4 currencies by 12M PR along with those currencies respective 1Y ZS. After the first filter, df2 should look like below:
df2 = {'Date': ('2015-10-6', '2015-10-7'),
'CAD 12M PR': (-7.4, -4.9),
'AUD 12M PR': (-2.3, -1.6),
'EUR 12M PR': (2.2, 4.7),
'GBP 12M PR': (-3.6, -2.5),
'CAD 12M ZS': (3.1, 2.5),
'AUD 12M ZS': (-1.7, 3.0),
'EUR 12M ZS': (-3.8, -3.7),
'GBP 12M ZS': (-1.6, -2.7),
}
After the filter has been applied, I want to retrieve the 2 currencies (from list that's been filtered) that have the lowest 1Y ZS. Of the filtered list above, AUD and EUR have the lowest 1Y ZS for both dates (but this could change). After this sort by 1y ZS, df3 should look like below:
df3 = {'Date': ('2015-10-6', '2015-10-7'),
'AUD 12M PR': (-2.3, -1.6),
'EUR 12M PR': (2.2, 4.7),
'AUD 12M ZS': (-1.7, 3.0),
'EUR 12M ZS': (-3.8, -3.7),
}
So the first filter finds the 4 currencies with the lowest 12M PR, and the 2nd sort finds the 2 currencies with the lowest 1Y ZS of the 4 currencies with the lowest 12M PR. But I don't know how to go from df to df3.
I can get the 4 currencies with the lowest 12M PR (df2) using the below code:
Short = {
'Short 1':
df[['CAD 12M PR', 'AUD 12M PR', 'NOK 12M PR', 'SEK 12M PR', 'NZD 12M PR', 'EUR 12M PR', 'GBP 12M PR', 'JPY 12M PR']].T.apply(lambda x: x.nsmallest(1).idxmax()).str[0:3],
'Short 2':
df[['CAD 12M PR', 'AUD 12M PR', 'NOK 12M PR', 'SEK 12M PR', 'NZD 12M PR', 'EUR 12M PR', 'GBP 12M PR', 'JPY 12M PR']].T.apply(lambda x: x.nsmallest(2).idxmax()).str[0:3],
'Short 3':
df[['CAD 12M PR', 'AUD 12M PR', 'NOK 12M PR', 'SEK 12M PR', 'NZD 12M PR', 'EUR 12M PR', 'GBP 12M PR', 'JPY 12M PR']].T.apply(lambda x: x.nsmallest(3).idxmax()).str[0:3],
'Short 4':
df[['CAD 12M PR', 'AUD 12M PR', 'NOK 12M PR', 'SEK 12M PR', 'NZD 12M PR', 'EUR 12M PR', 'GBP 12M PR', 'JPY 12M PR']].T.apply(lambda x: x.nsmallest(4).idxmax()).str[0:3],
'Short 1 12M PR':
df[['CAD 12M PR', 'AUD 12M PR', 'NOK 12M PR', 'SEK 12M PR', 'NZD 12M PR', 'EUR 12M PR', 'GBP 12M PR', 'JPY 12M PR']].apply(lambda row: row.nsmallest(1).values[-1],axis=1),
'Short 2 12M PR':
df[['CAD 12M PR', 'AUD 12M PR', 'NOK 12M PR', 'SEK 12M PR', 'NZD 12M PR', 'EUR 12M PR', 'GBP 12M PR', 'JPY 12M PR']].apply(lambda row: row.nsmallest(2).values[-1],axis=1),
'Short 3 12M PR':
df[['CAD 12M PR', 'AUD 12M PR', 'NOK 12M PR', 'SEK 12M PR', 'NZD 12M PR', 'EUR 12M PR', 'GBP 12M PR', 'JPY 12M PR']].apply(lambda row: row.nsmallest(3).values[-1],axis=1),
'Short 4 12M PR':
df[['CAD 12M PR', 'AUD 12M PR', 'NOK 12M PR', 'SEK 12M PR', 'NZD 12M PR', 'EUR 12M PR', 'GBP 12M PR', 'JPY 12M PR']].apply(lambda row: row.nsmallest(4).values[-1],axis=1),
}
Once I have the 4 currencies with the lowest 12M PR (df2), I don't know how to apply the last sort based on the filtered list (df2) to arrive at df3.