-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]]
3
  • 1
    I don't see any tuple in your final result Commented Dec 18, 2018 at 17:14
  • 1
    df[['col1', 'col2', 'col3']].values.tolist() Commented Dec 18, 2018 at 17:15
  • You can select specific columns and then convert to a list of lists. See the marked duplicates. Commented Dec 18, 2018 at 17:16

1 Answer 1

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]]
Sign up to request clarification or add additional context in comments.

1 Comment

this did it thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.