2

I have a large data file containing some column of hours like shown below

0

0

0

0

0

1

1

1

1

2

2

3

3 ... ....

This data continues like this until 23 then starts from zero again, so what i want to do here is to pick the first value from every group of same number so that i have output like

0

1

2

3

4

5

. . .

23

0

1

...

and so on, i'm not sure if my question is clear, this seem to be easy but i have been struggling to do it. Please note that an hour can be reapeted any often.

Mbali Aleh
  • 41
  • 6

1 Answers1

0

Unless I misunderstood the question something like below should do what you want.

# the function only appends a number to your `final list`,
# if the new number is different than the `final lists`s last element.
def appendIfNewNumber(unqNumbers, number):
    if len(unqNumbers) == 0 or number != unqNumbers[-1]:
        unqNumbers.append(number)


# instantiate some list
unqNumbers = []
someIterator = [1, 1, 1, 2, 3, 3, 4, 4, 2, 10] # for the sake of example.

# Assuming you are either reading numbers one by one from your file,
# or already have them stored in a list. 
# `someIterator` below is practically assumed to contain the elements of
# your first list - the one you'd like to filter.
for number in someIterator: # or list.
    appendIfNewNumber(unqNumbers, number)

print(unqNumbers) # --> [1, 2, 3, 4, 2, 10]
Muts
  • 145
  • 7
  • can you please comment each line of your code so that i will know how to apply in my case – Mbali Aleh Oct 14 '19 at 12:31
  • @MbaliAleh is there anything specific that's not clear? I can elaborate a bit more. – Muts Oct 14 '19 at 12:43
  • The only thing that worries me now is that, those first hours that i was picking using this code have corresponding values in other rows of my dataframe, so i actually want to pick the whole row that corresponds to that time, i am not sure if i'm clear enough.\ – Mbali Aleh Oct 16 '19 at 11:37