Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataFrame tolist for multiple column

How to do add a new column that contains a list of the current columns 'bar4': [[5,1,11],[6,2,22],[5,3,33]] in the following dataframe.

import pandas as pd

foo1 = (['L1','L1','L2'])
foo2 = ([5,5,6])
foo3 = ([1,1,2])

index = pd.MultiIndex.from_arrays(
    [foo1,foo2,foo3], names=['ifoo1','ifoo2','ifoo3']
    )
init = pd.DataFrame({
    'bar1': [5,6,5],
    'bar2': [1,2,3],
    'bar3': [11,22,33]
    }, index=index)

I initially thought it would be a similar operation to something init['barX'] = init.bar1 + init.bar2, but int['bar4'] = init.bar1, init.bar2, init.bar3 is definately not the solution.

Desired result:

 #                 bar1  bar2 bar3 bar4
 # foo1 foo2 foo3
 # L1   5    1      5    1    11   [5,1,11]
 # L1   5    1      6    2    22   [6,2,22]
 # L2   6    2      5    3    33   [5,3,33]
like image 707
crashMOGWAI Avatar asked Jun 17 '26 10:06

crashMOGWAI


1 Answers

I think you need convert values to numpy array by values with numpy.ndarray.tolist:

init['bar4'] = init.values.tolist()
print (init)
                   bar1  bar2  bar3        bar4
ifoo1 ifoo2 ifoo3                              
L1    5     1         5     1    11  [5, 1, 11]
            1         6     2    22  [6, 2, 22]
L2    6     2         5     3    33  [5, 3, 33]

And if need specify columns:

cols = ['bar1','bar2','bar3']
init['bar4'] = init[cols].values.tolist()
print (init)
                   bar1  bar2  bar3        bar4
ifoo1 ifoo2 ifoo3                              
L1    5     1         5     1    11  [5, 1, 11]
            1         6     2    22  [6, 2, 22]
L2    6     2         5     3    33  [5, 3, 33]
like image 55
jezrael Avatar answered Jun 18 '26 23:06

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!