Home > database >  Count occurence of an event regarding two intervals [Python]
Count occurence of an event regarding two intervals [Python]

Time:08-31

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 !

CodePudding user response:

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())
  • Related