Home > Software design >  Fastest way of creating and sorting the timestamp data with Python?
Fastest way of creating and sorting the timestamp data with Python?

Time:01-09

Lets say I will have two arrays. The first row would specify the timestamp and 2nd row would be data.

timeStamp = ['0001','0002','0003',...,'9999']

data = [6234,2372,1251,...,5172]

What would be the best way to store them? And let's say I would like to sort the data from smallest to bigger number with keeping their timestamp values attached to them?

CodePudding user response:

You could use a two-dimensional array. You can create this by using

timestamp_data = [ [timeStamp[i], data[i]] for i in range(len(timeStamp)) ]

Now, you can sort this by using

sorted_timestamp_data = sorted(timestamp_data, key=lambda row: row[1])

CodePudding user response:

A dictionary will work really well for you. You can zip data and timeStamp and sort by data then cast the tuples to dict (dictionaries preserve insertion order). Then you'll have data-timestamp pairs where data are keys and timestamps are values.

out = dict(sorted(zip(data, timeStamp)))

Output:

{1251: '0003', 2372: '0002', 5172: '9999', 6234: '0001'}

If you want 2 separate lists instead, you can do the following. Instead of casting to dict constructor, unpack to lists:

data[:], timeStamp[:] = zip(*sorted(zip(data,timeStamp)))

Output:

[1251, 2372, 5172, 6234], ['0003', '0002', '9999', '0001']

CodePudding user response:

Depends how you want to use it. If you want to go for no additonal library, I would use something like

result = sorted(({"timestamp": ts, "data": data} for ts, data in zip(timeStamp, data)), key=lambda d:d["data"]

That is basically a list of dictionaries sorted by data

CodePudding user response:

To organize the data in the way you described, you could simple do:

sorted(zip(timeStamp, data), key=lambda x: x[1])

or

from operator import itemgetter

sorted(zip(timeStamp, data), key=itemgetter(1))

To store this object, you could pickle it, and a good description is here. Obviously, there are a lot of options to store it.

CodePudding user response:

Multiple ways of doing this -

Using base python and zip

l = zip(timeStamp, data)

print(sorted(l, key=lambda x: x[0]))
[(1, 6234), (2, 2372), (3, 1251), (9, 1245), (9999, 5172)]

Using numpy and argsort

arr = np.stack([timeStamp, data])

print(arr[arr[:, 0].argsort()])
array([[   9,    1,    2,    3, 9999],
       [124
```5, 6234, 2372, 1251, 5172]])

Using pandas datafames and sort_values

import pandas as pd

df = pd.DataFrame(zip(timeStamp, data), columns=['timeStamp','data'])
print(df.sort_values('timeStamp'))
   timeStamp  data
1          1  6234
2          2  2372
3          3  1251
0          9  1245
4       9999  5172
  •  Tags:  
  • Related