0

I have a pandas dataframe with a single row and a certain set of columns. I would to add a new column, with a single cell, that contains a list of K numpy arrays. I obtained a series of K array by the following fragment of code, but it doesn't do what I want.

pd.Series([v for v in new_column])

How can I do it? "new_column" is a list of K numpy arrays.

UPDATE 1: this is new_column

new_column = [array([1,2,3]),array([4,5,6]),...]

I would obtain the following series (pycharm debugger variables style):

new_column = {Series(1,)}(0,[array(1,2,3), array(4,5,6),...])
2
  • 2
    Can you show us the sample data and expected output Commented Jul 17, 2020 at 17:21
  • OK, I update my question Commented Jul 17, 2020 at 17:22

1 Answer 1

1

Try this: you first create an empty pandas.Series of dtype 'object', and then you modify the value in the first position (index 0) by using the .at method.

new_column = [np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9])]

s = pd.Series(dtype='object')
s.at[0] = new_column

s
# 0    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# dtype: object
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.