lundi 22 mai 2017

Python DataFrame: Conditional Subset Based on Values in 3 Different Fields

I have the following Python dataframe shown below:

enter image description here

For each "Region" I have specific criteria for what Activities I want to show listed below:

a) For Region 1, I want to show the first three accounts with an "Activity" of meeting and the first two accounts with "Activity" of call

b) For Region 2, I want to show the first account with "Activity" of call and first account with "Activity" of meeting

c) For Region 4, I want to just show the top 6 Accounts by "Rank"

Below is the resultant dataframe I wan to get:

enter image description here

I can get the same number of meetings and calls by region using the code below. But I don't know how to get a subset of different meetings and calls based on Regional criteria.

d1 = data[data['Activity'] == 'meeting'].groupby('Region')\
       .apply(lambda x: x.sort_values('Rank')[:3])
d2 = data[data['Activity'] == 'call'].groupby('Region')\
       .apply(lambda x: x.sort_values('Rank')[:2])    
pd.concat([d1, d2])

Any help is greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire