1

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.

2
  • Do you need to create the intermidiate dictionary? Why not directly put the data into lists? Commented Jul 22, 2019 at 14:31
  • 1
    @DobromirM, By using csv reader and slices to create the lists directly? That sounds like it will work, I'll try it. Thanks. Commented Jul 22, 2019 at 14:41

2 Answers 2

2

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()]
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Thanks for the helpful info. These Python idioms are very clean. As a beginner programmer I am glad to have been introduced to them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.