2

I find it hard to convert pandas Series of size M each containing a numpy arrays each of size N into a matrix/numpy array/dataframe of size MxN

Example:

import pandas as pd
import numpy as np
from scipy import stats
d = pd.DataFrame({'grp': np.random.randint(1, 10, 1000), 'x':np.random.rand(1000,)})
s = d.groupby('grp')['x'].apply(lambda x: stats.gaussian_kde(x.values, bw_method = .01).evaluate(np.linspace(0,1,100)))

The output I get is of type Series where the type of the entries are numpy.ndarray. How do I convert this to be size of 10 (groups) times 100 (evaluation bins)?

1 Answer 1

2

You can use np.vstack for that

sArray = np.vstack(s)

But in your case your Series is (9,100). If you want to make it (10,100) you need to change the line.

d = pd.DataFrame({'grp': np.random.randint(1, 10, 1000), 'x':np.random.rand(1000,)})

to

d = pd.DataFrame({'grp': np.random.randint(0, 10, 1000), 'x':np.random.rand(1000,)})

or

d = pd.DataFrame({'grp': np.random.randint(1, 11, 1000), 'x':np.random.rand(1000,)})

Not sure what you want to do there.

Cheers

Sign up to request clarification or add additional context in comments.

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.