I have the following dataframe in Pandas:
| name | value | in | out |
|---|---|---|---|
| A | 50 | 1 | 0 |
| A | -20 | 0 | 1 |
| B | 150 | 1 | 0 |
| C | 10 | 1 | 0 |
| D | 500 | 1 | 0 |
| D | -250 | 0 | 1 |
| E | 800 | 1 | 0 |
There are maximally only 2 observations for each name: one for in and one for out.
If there is only in for a name there is only one observation for it.
You can create this dataset with this code:
data = {
'name': ['A','A','B','C','D','D','E'],
'values': [50,-20,150,10,500,-250,800],
'in': [1,0,1,1,1,0,1],
'out': [0,1,0,0,0,1,0]
}
df = pd.DataFrame.from_dict(data)
I want to sum the value column for each name but only if name has both in and out record. In other words, only when one unique name has exactly 2 rows.
The result should look like this:
| name | value |
|---|---|
| A | 30 |
| D | 250 |
If I run the following code I got all the results without filtering based on in and out.
df.groupby('name').sum()
| name | value |
|---|---|
| A | 30 |
| B | 150 |
| C | 10 |
| D | 250 |
| E | 800 |
How to add the beforementioned filtering based on columns?