I have a pandas df that looks like this:

0   matrix([[1], [2], [3]])
1   matrix([[2], [3], [4]])
2   matrix([[0], [0], [0]])
999 matrix([[2], [1], [3]])

And I want to access the first entry of df['beta'], idealy either a list or a np array that looks like: [1, 2, 0, ..., 2]

What I have tried so far:

Convert the series to a list of lists

b_t = list(df['beta_t'].apply(lambda x: x.flatten().tolist()[0]))
b_t = [row[0] for row in b_t]

This works, but it takes a while in large datasets.

I also tried:

b_t = list(df['beta_t'].apply(lambda x: np.array(x.flatten()[0])))

but this results in a series of series and I don't know how to continue from here.

Any suggestions on how I should improve my codes? Thanks in advance!

1 Answers

jezrael On

I believe you need:

df['beta_t'].apply(lambda x: x[0][0][0])


[x[0][0][0] for x in df['beta_t']]