kbe
kbe

Reputation: 17

How to convert a csv file with date index and tuple of int values to dict suitable for plotting?

Here is a sample of the csv data:

Timestamp,Systolic,Diastolic,Pulse
201711051037,135,81,62
201711061614,121,74,60
201711150922,129,74,70
201711170901,135,80,65

I am using Python 3.6.8 and this script to create a dict:

from csv import DictReader as dict_reader

d = dict()

with open(CSV_FILE, 'r') as csvfile:
    reader = dict_reader(csvfile)
    ts, sy, dia, p = next(reader)
    for r in reader:
        d[r[ts]] = int(r[sy]), int(r[dia]), int(r[p])

The result is:

{'201711051037': (135, 81, 62), '201711061614': (121, 74, 60), '201711150922': (129, 74, 70), '201711170901': (135, 80, 65)}

I would like to convert the result into a form that can be used with Matplotlib plot. Possibly something like:

Timestamp = [201711051037, 201711061614, 201711150922, 201711170901]
Systolic = [135, 121, 129, 135]
Diastolic = [81, 74, 74, 80]
Pulse = [62, 60, 70, 65]

In order to create a line plot with Matplotlib and use fills and other features, I believe the data needs to be in separate lists. I have tried different things like:

index = list(d.keys())
data = list(d.values())
sy = [data[i][j] for i in data for j in data]

And also looked at these similar questions:

Make dicts read from a csv file ordered dicts

Python matplotlib plot dict with multiple values

Plotting a dictionary with multiple values per key

However, I am stuck at this point. Any hints or directions would be greatly appreciated. Thanks.

Upvotes: 1

Views: 102

Answers (2)

Sheldore
Sheldore

Reputation: 39072

In case you are interested in alternative solution, you can use * to unpack the dictionary values and then use zip to get three different lists. Here I am using map to convert the output into lists, which is what you want.

dic = {'201711051037': (135, 81, 62), '201711061614': (121, 74, 60), 
       '201711150922': (129, 74, 70), '201711170901': (135, 80, 65)}


Timestamp = list(map(int, dic.keys()))
Systolic, Diastolic, Pulse = map(list, zip(*dic.values())) 

print (Timestamp)
print (Systolic)
print (Diastolic)
print (Pulse)

# [201711051037, 201711061614, 201711150922, 201711170901]
# [135, 121, 129, 135]
# [81, 74, 74, 80]
# [62, 60, 70, 65]

Moreover, if you have to just plot them, you don't need to convert the values into lists

Timestamp = list(map(int, dic.keys()))
Systolic, Diastolic, Pulse = zip(*dic.values())

plt.plot(Timestamp, Systolic, label='Systolic')
plt.plot(Timestamp, Diastolic, label='Diastolic')
plt.plot(Timestamp, Pulse, label='Pulse')
plt.legend()

enter image description here

Upvotes: 1

Ben Quigley
Ben Quigley

Reputation: 737

Starting with what you want the output to look like:

Timestamp = [201711051037, 201711061614, 201711150922, 201711170901]
Systolic = [135, 121, 129, 135]
Diastolic = [81, 74, 74, 80]
Pulse = [62, 60, 70, 65]

You can get these from:

Timestamp = [int(stamp) for stamp in d.keys()]
Systolic = [value[0] for value in d.values()]
Diastolic = [value[1] for value in d.values()]
Pulse = [value[2] for value in d.values()]

Upvotes: 2

Related Questions