Iterate over a random timestamp column in Python

389 views Asked by At

I have a huge file, where the columns are randomly sequenced and I was able to use pandas to read a specific timestamp column.

with open(<file name>, 'r') as in_file:
     b = pandas.read_csv(in_file,usecols=["<Timestamp Column Name>"],parse_dates=[0])

c = b.to_csv(header=None,index=False)
print c

Sample values are:

2016-11-26 16:12:00
2016-11-30 09:19:00
2016-11-28 17:30:00
2016-11-30 14:50:00
2016-11-26 21:39:00
2016-01-12 11:14:00
2016-11-27 09:39:00
2016-11-28 06:50:00
2016-11-26 14:08:00
2016-11-30 12:42:00
2016-11-26 11:01:00
2016-11-25 08:23:00

Now I'm trying to loop through this column to achieve a count. If I use a simple for loop, it reads every digit like a string, which is of no use. Appreciate some help.

1

There are 1 answers

1
宏杰李 On BEST ANSWER
text='''2016-11-26 16:12:00
2016-11-30 09:19:00
2016-11-28 17:30:00
2016-11-30 14:50:00
2016-11-26 21:39:00
2016-01-12 11:14:00
2016-11-27 09:39:00
2016-11-28 06:50:00
2016-11-26 14:08:00
2016-11-30 12:42:00
2016-11-26 11:01:00
2016-11-25 08:23:00'''

l = text.splitlines()

or :

l = text.split('\n')

or read from csv file:

with open('you_data.csv') as f:
    l = f.readlines()

out:

print(l)
['2016-11-26 16:12:00', '2016-11-30 09:19:00', '2016-11-28 17:30:00', '2016-11-30 14:50:00', '2016-11-26 21:39:00', '2016-01-12 11:14:00', '2016-11-27 09:39:00', '2016-11-28 06:50:00', '2016-11-26 14:08:00', '2016-11-30 12:42:00', '2016-11-26 11:01:00', '2016-11-25 08:23:00']

just split by line break use splitlines, this will return a list