0

I have a dictionary of dictionaries in python like the following example:

example:

result = {
    1: {'A': 11472, 'C': 8405, 'T': 11428, 'G': 6613}, 
    2: {'A': 11678, 'C': 9388, 'T': 10262, 'G': 6590}, 
    3: {'A': 2945, 'C': 25843, 'T': 6980, 'G': 2150}, 
    4: {'A': 1149, 'C': 24552, 'T': 7000, 'G': 5217}, 
    5: {'A': 27373, 'C': 3166, 'T': 4494, 'G': 2885}, 
    6: {'A': 19300, 'C': 4252, 'T': 7510, 'G': 6856}, 
    7: {'A': 17744, 'C': 5390, 'T': 7472, 'G': 7312}
}

and I want to make a numpy array in python using the values of inner dictionaries. in fact in the example there are 7 inner dictionaries with 4 items so, in the array thet would be 7 lists with 4 items. so, every item in the in the numpy array is one of the values in the inner dictionaries. for instance this is the 1st inner dictionary:

{'A': 11472, 'C': 8405, 'T': 11428, 'G': 6613}

and this would be the 1st list in the numpy array:

[ 11472,  8405,  11428,  6613]

I am trying to do that in python using the following line but it does not return expected output.

import pandas as pd
df = pandas.DataFrame(result, index=[0])

do you know how to fix it?

here is the expected output:

array([[ 11472,  8405,  11428,  6613],
       [ 11678,  9388,  10262 ,  6590],
       [ 2945,  25843,  6980 ,  2150],
       [ 1149,  24552,  7000 ,  5217],
       [ 27373,  3166,  4494 ,  2885],
       [ 19300,  4252,  7510,  6856],
       [ 17744,  5390,  7472,  7312]])
0

2 Answers 2

1

Requires Python 3. Also, the values must be inserted in the ACTG order.

>>> np.array([list(val.values()) for val in result.values()])
array([[11472,  8405, 11428,  6613],
       [11678,  9388, 10262,  6590],
       [ 2945, 25843,  6980,  2150],
       [ 1149, 24552,  7000,  5217],
       [27373,  3166,  4494,  2885],
       [19300,  4252,  7510,  6856],
       [17744,  5390,  7472,  7312]])
Sign up to request clarification or add additional context in comments.

5 Comments

in python 2.7 also works.
Dicts are not order in python 2.7
@StephenRauch Dicts weren't ordered prior to Python 3.7, either.
@Tomothy32, very true, except for CPython got a jump on the rest... :-)
@StephenRauch That's true, except in 3.6 it was considered an "implementation detail and should not be relied upon".
0

A couple of comprehensions can do that like:

Code:

data = [[result[idx][k] for k in 'ACTG']
        for idx in sorted(result)
        ]

an_array = np.array(data)

Test Code:

result = {1: {'A': 11472, 'C': 8405, 'T': 11428, 'G': 6613},
          2: {'A': 11678, 'C': 9388, 'T': 10262, 'G': 6590},
          3: {'A': 2945, 'C': 25843, 'T': 6980, 'G': 2150},
          4: {'A': 1149, 'C': 24552, 'T': 7000, 'G': 5217},
          5: {'A': 27373, 'C': 3166, 'T': 4494, 'G': 2885},
          6: {'A': 19300, 'C': 4252, 'T': 7510, 'G': 6856},
          7: {'A': 17744, 'C': 5390, 'T': 7472, 'G': 7312}}

data = [[result[idx][k] for k in 'ACTG']
        for idx in sorted(result)
        ]

print(data)

Results:

[[11472, 8405, 11428, 6613], 
 [11678, 9388, 10262, 6590], 
 [2945, 25843, 6980, 2150], 
 [1149, 24552, 7000, 5217], 
 [27373, 3166, 4494, 2885], 
 [19300, 4252, 7510, 6856], 
 [17744, 5390, 7472, 7312]]

1 Comment

thanks. this one returns a list of lists. but I want a numpy array. do you know how to do that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.