-1

I have df that looks like this:

col1     col2      col3     col4
1         0         1        0 
1         0         1        0 
1         0         1        0 
1         0         1        0 
1         0         1        0 

How do I convert col1-col3 into a list of list of tuples and assign to variable? and then create an array of only col4 and assign to variable?

example final result:

lst = [[1,0,1],[1,0,1],[1,0,1],[1,0,1],[1,0,1]]]
lst_col4 = [[0],[0],[0],[0],[0]]
RustyShackleford
  • 3,462
  • 9
  • 40
  • 81

1 Answers1

4

Just index those coluns, and use the .values attribute:

lst = df[[col1, col2, col3]].values.tolist()

Example:

>>> df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9]})
>>> lst = df[['a', 'c']].values.tolist()
>>> print(lst)
 [[1, 7], [2, 8],[3, 9]]
Tim
  • 2,756
  • 1
  • 15
  • 31