I am attempting to combine elements of a dataframe into a nested list. Say I have the following:
df = pd.DataFrame(np.random.randn(100,4), columns=list('abcd'))
df.head(4)
a b c d
0 0.455258 1.135895 0.573383 -0.637943
1 0.262079 -0.397168 -0.980062 -1.600837
2 0.921582 0.767232 -0.298590 -0.159964
3 -0.645110 -0.709058 1.223899 0.382212
Then, I would like to create a fifth column e that looks like:
a b c d e
0 0.455258 1.135895 0.573383 -0.637943 [[0.455258 1.135895 0.573383 -0.637943]]
1 0.262079 -0.397168 -0.980062 -1.600837 [[0.262079 -0.397168 -0.980062 -1.600837]]
2 0.921582 0.767232 -0.298590 -0.159964 [[0.921582 0.767232 -0.298590 -0.159964]]
3 -0.645110 -0.709058 1.223899 0.382212 [[-0.645110 -0.709058 1.223899 0.382212]]
efficiently.
My most efficient but wrong guess so far has been to do
df['e'] = df.values.tolist()
But that just results in:
a b c d e
0 0.455258 1.135895 0.573383 -0.637943 [0.455258 1.135895 0.573383 -0.637943]
1 0.262079 -0.397168 -0.980062 -1.600837 [0.262079 -0.397168 -0.980062 -1.600837]
2 0.921582 0.767232 -0.298590 -0.159964 [0.921582 0.767232 -0.298590 -0.159964]
3 -0.645110 -0.709058 1.223899 0.382212 [-0.645110 -0.709058 1.223899 0.382212]
My least efficient but correct guess has been:
a = []
for index, row in df.iterrows():
a.append([[row['a'],row['b'],row['c'],row['d']]])
Is there a better way?