emax emax - 6 months ago 23
Python Question

Python: how to extract only values from dict?

I created a dictionary

d
, where
A
is a list containing data of a
class


class Driver(object):
def __init__(self, name, raw_value, Id1):
self.name = name
self.raw = raw_value
self.Indicator1 = Id1

d = {}
for i in range(0,len(A)):
if not A[i].name in d:
d[A[i].name] = [A[i].Indicator1]
else:
d[A[i].name] += [A[i].Indicator1]


so

d[driver[0]] =

[0 0.002862
1 -15.005252
2 0.009653
3 -15.192950
4 14.941727
5 44.977511
Name: 3, dtype: float64,
0 0.005229
1 -0.230766
2 14.803119
3 -0.005263
Name: 3, dtype: float64,
0 -3.756292
1 14.827995
2 45.595015
3 44.964029
4 14.820670
Name: 3, dtype: float64]


as shown also in the picture from the my
notebook


enter image description here

and I want get only the values from that, so:

v = [0.002862, -15.005252, 0.009653,-15.192950, 14.941727, ..., 44.964029, 4.820670]

Answer

Try this:

import pandas as pd

pd.concat(d[users[0]]).values

this will concatenate your list of pandas Series (or numpy arrays) into a single one and then you can apply values accessor in order to get a numpy array or a .tolist() function in order to get a regular Python list