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)

4
  • Can you show what you are expecting as the end result ? Commented Apr 17, 2021 at 5: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'] Commented Apr 17, 2021 at 5: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. Commented Apr 17, 2021 at 6:02
  • You probably want a NumPy "structured array" which you can see how to create here: stackoverflow.com/questions/15579649/… Commented Apr 17, 2021 at 6:02

2 Answers 2

6

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

Comments

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']]

Comments

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.