Search code examples
pythonpandasanalytics

Group by with condition - Pandas


I need to filter some information from my data grid, such as average surviving age and proportion of living men and women

I try with this

tit.groupby(by=['Survived'])['Yes']

But I can't filter this way


Solution

  • You are probably working with the titanic dataset, and for this filter you don't need groupby.

    First filter:

    tit.loc[tit['Survived'] == 'Yes']
    

    Second filter:

    tit.loc[(tit['Survived'] == 'Yes') & (tit['Sex'] == 'Women')]