Learner
Learner

Reputation: 157

Adding a list of lists in a pandas dataframe column

I have a data frame like this

A  B   C 
0  1   2
1  6   5

I need to add a list of lists in the 4th column of the dataframe . So that it becomes

A  B   C   D
0  1   2   [[4,-0.05],[0.03,[-0.02,0.02]],[0.01,-0.03]]
1  6   5   [[4,-0.35],[0.07,[-0.02,0.02]],[0.91,-0.03]]   

Please note the list of lists in column D. I am unable to find a way to add this type of column data to pandas dataframe

Appreciate any help. Thanks in advance

Upvotes: 0

Views: 3286

Answers (1)

Zero
Zero

Reputation: 76927

You could create new Series as

In [11]: df['D'] = pd.Series([[[4,-0.05],[0.03,[-0.02,0.02]],[0.01,-0.03]],
                              [[4,-0.35],[0.07,[-0.02,0.02]],[0.91,-0.03]]])
In [12]: df
Out[12]:
   A  B  C                                                  D
0  0  1  2  [[4, -0.05], [0.03, [-0.02, 0.02]], [0.01, -0....
1  1  6  5  [[4, -0.35], [0.07, [-0.02, 0.02]], [0.91, -0....

However, I'm not sure, why you would want to store data in this structure!

Upvotes: 1

Related Questions