Pandas - concatenate column values ​​into a list in a new column

I have Python Pandas dataframe df:

d=[['hello',1,'GOOD','long.kw'],
   [1.2,'chipotle',np.nan,'bingo'],
   ['various',np.nan,3000,123.456]]                                                    
t=pd.DataFrame(data=d, columns=['A','B','C','D']) 

      

which looks like this:

print(t)
         A         B     C        D
0    hello         1  GOOD  long.kw
1      1.2  chipotle   NaN    bingo
2  various       NaN  3000  123.456

      

I'm trying to create a new column that is the list

values A

, B

, C

and D

. So it will look like this:

t['combined']                                             

Out[125]: 
0        [hello, 1, GOOD, long.kw]
1        [1.2, chipotle, nan, bingo]
2        [various, nan, 3000, 123.456]
Name: combined, dtype: object

      

I am trying to use this code:

t['combined'] = t.apply(lambda x: list([x['A'],
                                        x['B'],
                                        x['C'],
                                        x['D']]),axis=1)    

      

What this error returns:

ValueError: Wrong number of items passed 4, placement implies 1 

      

What puzzles me is to remove one of the columns that I want to put in the list (or add another column to the dataframe that I am NOT adding to the list), my code works.

For example, run this code:

t['combined'] = t.apply(lambda x: list([x['A'],
                                        x['B'],
                                        x['D']]),axis=1)      

      

Returns this, which is ideal if I only want three columns:

print(t)
         A         B     C        D                 combined
0    hello         1  GOOD  long.kw      [hello, 1, long.kw]
1      1.2  chipotle   NaN    bingo   [1.2, chipotle, bingo]
2  various       NaN  3000  123.456  [various, nan, 123.456]

      

I am at a complete loss as to why a combo box query would be done from all columns in the dataframe, throw an error, but select all but 1 column to create a combo box and the list will be generated as expected.

+3


source to share


1 answer


try this:



t['combined']= t.values.tolist()

t
Out[50]: 
         A         B     C        D                       combined
0    hello         1  GOOD  long.kw      [hello, 1, GOOD, long.kw]
1     1.20  chipotle   NaN    bingo    [1.2, chipotle, nan, bingo]
2  various       NaN  3000   123.46  [various, nan, 3000, 123.456]

      

+6


source







All Articles