Count occurence of an event regarding two intervals [Python]

Question:

Sorry if the title of my question is unclear, I had trouble finding an appropriate title for my problem. I am new to Python programming and trying to learn data analysis on my own.

I have the following pandas data frame :

|Event| y | x |bins_x  |bins_y  |
|:---:|:-:|:-:|:------:|:------:|
|0    |80 |34 |(20, 40]|(70, 80]|
|1    |78 |40 |(20, 40]|(70, 80]|
|2    |79 |32 |(20, 40]|(70, 80]|
|3    |80 |36 |(20, 40]|(70, 80]|
|4    |72 |44 |(40, 60]|(70, 80]|
|5    |77 |57 |(40, 60]|(70, 80]|
|...  |...|...|...     |...     |

I would like to count the occurrence of an event, by taking into account bins_x and bins_y values. For example, in the data frame above I have,

4 events where bins_x = (20, 40] and bins_y = (70, 80]
2 events where bins_y = (40, 60] and bins_y = (70, 80]

What would you use in the code to determine this ?

Thank you !

Asked By: Mitsun0bu

||

Answers:

That’s fairly simple, just use .value_counts() on a slice of the dataframe:

df = pd.DataFrame({'Event': [0,1,2,3,4,5],
                   'y': [80,78,79,80,72,77],
                   'x': [34,40,32,36,44,57],
                   'bins_x': [(20,40),(20,40),(20,40),(20,40),(40,60),(40,60)],
                   'bins_y': [(70,80),(70,80),(70,80),(70,80),(70,80),(70,80)]})
print(df[['bins_x', 'bins_y']].value_counts())
Answered By: max_jump
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.