Posts: 53
Threads: 25
Joined: Jan 2018
My data is as below:
[0,1,1,1,1,2,2,2,...85,85,85,80,80,80,...0,1,1,1,2,2,2,....85,85,95,80,80,80,...]
I want to be able to count how many times no 85 occurred and in this case it should return twice where I am just interested to know the point where it changes to 85. Using counts, or value_counts will result in the total number of occurrences but that's not what I am looking for. Thanks for the help in advance.
Posts: 46
Threads: 0
Joined: Jul 2019
Just make a cycle through the list and check for conditions you need manually.
Posts: 132
Threads: 35
Joined: Apr 2019
Aug072019, 12:17 PM
(This post was last modified: Aug072019, 12:17 PM by paul18fr.)
is it what you're looking for?
(just adapt to your case)
n = 600;
A = np.random.randint(100, size = (n,1), dtype = np.int);
loc = np.where(A == 85);
loc_exact = list(loc)[0];
Posts: 53
Threads: 25
Joined: Jan 2018
(Aug072019, 12:17 PM)paul18fr Wrote: is it what you're looking for?
(just adapt to your case)
n = 600;
A = np.random.randint(100, size = (n,1), dtype = np.int);
loc = np.where(A == 85);
loc_exact = list(loc)[0];
Thanks but it doesn't quite achieve what I am looking for. It is tricky because if I adapt this code, the result I will get is 6 occurrences of 85 instead of 2 occurrences. I am still thinking :
Posts: 360
Threads: 5
Joined: Jun 2019
Aug072019, 01:12 PM
(This post was last modified: Aug072019, 01:18 PM by ThomasL.)
(Aug072019, 12:17 PM)paul18fr Wrote: n = 600;
A = np.random.randint(100, size = (n,1), dtype = np.int);
loc = np.where(A == 85);
loc_exact = list(loc)[0]; please explain the usage of ; at the end of each line
This is python and not javascript!
(Aug072019, 01:04 PM)python_newbie09 Wrote: Thanks but it doesn't quite achieve what I am looking for. It is tricky because if I adapt this code, the result I will get is 6 occurrences of 85 instead of 2 occurrences. I am still thinking : With that code you get the indices where in your list the number 85 occurs.
If you only want the first occurence after a bunch of other (smaller) numbers than look at the differences of the indices. If you get e.g. [37,38,39,111,112,114] then 37 and 111 are your numbers. That should be doable.
Posts: 132
Threads: 35
Joined: Apr 2019
(Aug072019, 01:12 PM)ThomasL Wrote: please explain the usage of ; at the end of each line
This is python and not javascript! habit coming from Mathlab (BTW it has no consequence)
I didn't understand that you want to get the number of occurences of "...85" (i.e the dots AND 85)
Posts: 360
Threads: 5
Joined: Jun 2019
(Aug072019, 01:26 PM)paul18fr Wrote: habit coming from Mathlab (BTW it has no consequence)
I didn't understand that you want to get the number of occurences of "...85" (i.e the dots AND 85) a bad habit and yes i know it has no consequence as this is the reason you can code like this
if it would not work you would not use ; anymore. ;)
I´m not the threadstarter so i don´t want anything. :)
and i assume the dots should represent a bunch of numbers coming before the 85,
so there aren´t any strings in a list of numbers.
Posts: 53
Threads: 25
Joined: Jan 2018
(Aug072019, 01:26 PM)paul18fr Wrote: (Aug072019, 01:12 PM)ThomasL Wrote: please explain the usage of ; at the end of each line
This is python and not javascript! habit coming from Mathlab (BTW it has no consequence)
I didn't understand that you want to get the number of occurences of "...85" (i.e the dots AND 85)
yes, because the list is very long so I also cannot use the method of just counting the difference as suggested above for 1000 plus rows of time series data. To make it simpler, the list below as an example:
n = [0,1,1,1,1,2,2,2,85,85,85,80,80,80,0,1,1,1,2,2,2,85,85,85,80,80,80]
I tried to use the code below with the logic of first checking if the value is 85 and if the previous value is not 85 and the next value is the same as 85 then count 1. But I am getting an error; AttributeError: 'int' object has no attribute 'iloc'
this code does not include the counting part yet.
for i, x in enumerate(n):
if x==85 and x.loc[i1]!= 85 and x.iloc[i+1]==85:
print(i)
Posts: 53
Threads: 25
Joined: Jan 2018
(Aug072019, 01:35 PM)python_newbie09 Wrote: (Aug072019, 01:26 PM)paul18fr Wrote: habit coming from Mathlab (BTW it has no consequence)
I didn't understand that you want to get the number of occurences of "...85" (i.e the dots AND 85)
yes, because the list is very long so I also cannot use the method of just counting the difference as suggested above for 1000 plus rows of time series data. To make it simpler, the list below as an example:
n = [0,1,1,1,1,2,2,2,85,85,85,80,80,80,0,1,1,1,2,2,2,85,85,85,80,80,80]
I tried to use the code below with the logic of first checking if the value is 85 and if the previous value is not 85 and the next value is the same as 85 then count 1. But I am getting an error; AttributeError: 'int' object has no attribute 'iloc'
this code does not include the counting part yet.
for i, x in enumerate(n):
if x==85 and x.loc[i1]!= 85 and x.iloc[i+1]==85:
print(i)
I could probably live with the solution below for now but I need to make sure that it checks until the end of the list.
a = 0
for i, x in enumerate(n):
#print(n[i])
#print(n[i1])
#print(n[i+1])
if x ==85:
#print(n[i])
if n[i1]!=85:
#print(n[i1])
if n[i+1]==85:
#print(n[i+1])
a+=1
print(a)
Posts: 132
Threads: 35
Joined: Apr 2019
Aug072019, 05:25 PM
(This post was last modified: Aug072019, 05:25 PM by paul18fr.)
Is the following what you were looking for at the beginning?
Paul
import numpy as np
##I build a model
n = 20
A = []
for i in range(n):
A.append("1")
A[3] = '...1'
A[6] = '...1'
## The list is converted to an array
A = np.array(A)
loc = np.where(A == "...1")
exact_loc = list(loc)[0] # Eureka
