问题
I have a dataframe (called df), where there is a time series with a time stamp (first column) and several integer data columns.
TimeStamp Country 1 Country 2
12:00:00 10.05 21.60
11:59:00 11.12 22.33
11:58:00 12.18 21.70
11:57:00 11.70 21.60
11:56:00 11.65 22.33
11:55:00 11.70 21.60
11:54:00 11.50 22.33
11:53:00 11.80 21.80
... ... ...
Problem: I'd like to count the number of occurrences of a the maximum in a specific range (not the whole axis!).
E.g. In column Country 2, I'd like to count the number of occurrences of the max value in rows 1-8. So first I find the max value: df.iloc[0:7,1].max() -> 22.33
and now I'd like to count them, how to do it?
I'm looking for something like count(range, target value)
-> df.count(df.iloc[0:7,1)], df.iloc[0:7,1].max())
The output should be an integer. Here the max value (which is 22.33) occurs 3 times in the defined range, so I'd expect 3.
Thanks for your help
回答1:
Compare each value of filtered Series by maximum and count True
s values by sum
:
s = df.iloc[0:7,1]
count = s.eq(s.max()).sum()
#alternative
count = (s == s.max()).sum()
print (count)
3
EDIT: Use Series.between:
s = df.iloc[0:7,1]
thr = 0.01
#print (s.max() - thr)
#print (s.max() + thr)
count = s.between(s.max() - thr, s.max() + thr).sum()
print (count)
3
来源:https://stackoverflow.com/questions/61287698/python-dataframe-count-occurrences-in-a-specified-range-not-axis