turn columns into one column as list python 1

turn columns into one column as list python

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]

Here is what the above code is Doing:
1. Create a new column called ‘combined’
2. Use the .values attribute to get the values of the dataframe as a numpy array
3. Use the .tolist() method to convert the numpy array to a list
4. Assign the list to the new column

Similar Posts