0

im trying to convert a data dictionary with a structure as below:

{'name': array(['Ben','Sean,'Fred'])
 'age': array([22, 16, 35]),
 'marks': array([98, 75, 60]),
 'result': array('HD','D','C')}

I need to then filter out the dictionary to only include name, mark and result in the new numpy array to be able to plot on a graph (i can do this but cant for the life of me filter the list and then convert to numpy)

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Slacky88
  • 67
  • 4
  • Can you show what you are expecting as the end result ? – Hitesh Kumar Apr 17 '21 at 05:53
  • Sorry original question was meant to be an array with the name, mark and result column, so ignoring the birthday column. So something like array = ['Ben', 'Sean', 'Fred'][98, 75, 60]['HD', 'D', 'C'] – Slacky88 Apr 17 '21 at 05:59
  • Do you need your numpy array to have nested arrays i.e value of name, marks and result ? Just make it clear how your numpy array should look like. – Hitesh Kumar Apr 17 '21 at 06:02
  • You probably want a NumPy "structured array" which you can see how to create here: https://stackoverflow.com/questions/15579649/python-dict-to-numpy-structured-array – John Zwinck Apr 17 '21 at 06:02

2 Answers2

5

Let's assume your dictionary is something like this.

dict = {
'name': ['Ben','Sean','Fred'],
'age': [22, 16, 35], 
'marks': [98, 75, 60], 
'result': ['HD','D','C']
}

You can iterate over the dictionary to get desired values and append them into a list. Then convert it into a NumPy array. Here I am using all of the keys

name, age, marks, result

but you can filter some keys if you like.

if key not in ['age']:

import numpy as np
data_list = []
for key, val in dict.items():
    data_list.append(val)

numpy_array = np.array(data_list)
transpose = numpy_array.T
transpose_list = transpose.tolist()

The end result will be following:

[['Ben', '22', '98', 'HD'],
['Sean', '16', '75', 'D'],
['Fred', '35', '60', 'C']]
Muhammad Umar
  • 130
  • 1
  • 10
0

You can try pandas

import pandas as pd

d = {
    'name': ['Ben','Sean','Fred'],
    'age': [22, 16, 35],
    'marks': [98, 75, 60],
    'result': ['HD','D','C']
}

df = pd.DataFrame(d)

result = df[['name', 'marks', 'result']].T.values
print(type(result))
print(result)

<class 'numpy.ndarray'>
[['Ben' 'Sean' 'Fred']
 [98 75 60]
 ['HD' 'D' 'C']]
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52