Groupby when values on different columns are True

Viewed 42

I have a dataframe:

data = {'first_column':  ['first_value', 'second_value', ...],
        'second_column': ['yes', 'no', ...],
        'third_column':  ['first_value', 'second_value', ...],
        'fourth_column': ['yes', 'no', ...],
        }

I'm trying to groupby 'first_column', when values in 'second_column' and 'fourth_column' == 'yes' and I get an error: "TypeError: unsupported operand type(s) for &: 'list' and 'list' "

I receive no errors when the condition is set only to one column:

*data.groupby([data['second_column']=='yes'])[['first_column', 'third_column']].mean()*

but when I try to add the "&" operator is when it fails:

*data.groupby(([data['second_column']=='yes']) & ([data['fourth_column']=='yes']))[['first_column', 'third_column']].mean()*

Is there a workaround here? Thanks!

1 Answers

You are not looking for groupby but rather to filter and get the mean of the remaining records -

data[data['second_column']=='yes'][['first_column', 'third_column']].mean()
Related