Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an array from two columns in pandas

suppose I have a DataFrame similar to this:

d = {'col1': [0, 2, 4], 'col2': [1, 3, 5], 'col3': [2, 4, 8]}
df = pd.DataFrame(d)

   col1  col2  col3
0     0     1     2
1     2     3     4
2     4     5     8

How can I select col1 and col2 and turn them into this array?

array([[0, 1],
       [2, 3],
       [4, 5]])
like image 233
Clement Attlee Avatar asked Apr 01 '17 18:04

Clement Attlee


2 Answers

You can access the underlying numpy array via the to_numpy method:

df[['col1', 'col2']].to_numpy()
Out: 
array([[0, 1],
       [2, 3],
       [4, 5]])

.values attribute will do the same if you are on an earlier version (before v0.24).

like image 170
ayhan Avatar answered Sep 24 '22 23:09

ayhan


You can also achieve the same output with the below code.

import numpy as np
np.array(df[['col1','col2']])
Out[60]: 
array([[0, 1],
       [2, 3],
       [4, 5]])
like image 24
Mohammad Akhtar Avatar answered Sep 22 '22 23:09

Mohammad Akhtar