3
d = [{'Number':'0001',  'Name':'A','Course':'Eng','Score':81 },{'Number':'0001',  'Name':'A','Course':'Geo','Score':75},
       {'Number':'0002',  'Name':'B','Course':'Eng','Score': 76} , {'Number':'0002',  'Name':'B','Course':'Geo','Score':90 },
    {'Number':'0003',  'Name':'C','Course':'Eng','Score':81 },{'Number':'0003',  'Name':'C','Course':'Geo','Score':100, },
     {'Number':'0003',  'Name':'C','Course':'Bio','Score':90 }]


data = pd.DataFrame(d, index=[1,2,3,4,5,6,7])
data

Course  Name    Number  Score
1   Eng A   0001    81
2   Geo A   0001    75
3   Eng B   0002    76
4   Geo B   0002    90
5   Eng C   0003    81
6   Geo C   0003    100
7   Bio C   0003    90

what I want is return only name C because C is the only one who score over 80 on every subject

enter image description here

0

5 Answers 5

3

you can use filter after groupby such as:

print (data.groupby('Name').filter(lambda x: (x['Score'].min()>80)))

and you get

  Course Name Number  Score
5    Eng    C   0003     81
6    Geo    C   0003    100
7    Bio    C   0003     90
Sign up to request clarification or add additional context in comments.

Comments

3

A groupby is what you want

grouped = data.groupby('Name').min()
print(grouped.loc[grouped['Score']>80].index[0])

This will give you

Out[1]: ['C']

Comments

1

Using transform with all

data.loc[data.Score.gt(80).groupby(data['Name']).transform('all'),'Name'].unique()
Out[9]: array(['C'], dtype=object)

Comments

0

You can use transform with unique

data.loc[data.groupby('Name')['Score'].transform('min')>80].Name.unique()

Output:

array(['C'], dtype=object)

Comments

0

One other way may be as following:

f = lambda group: group.name if all(group>80) else None
r = data.groupby(['Name'])['Score'].apply(f).dropna().values
print(r)

# Result: ['C']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.