如何从pandas中的两列创建一个数组

Cle*_*lee 4 python arrays pandas

假设我有一个类似于此的DataFrame:

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
Run Code Online (Sandbox Code Playgroud)

如何选择col1和col2并将它们转换为此数组?

array([[0, 1],
       [2, 3],
       [4, 5]])
Run Code Online (Sandbox Code Playgroud)

ayh*_*han 10

您可以通过以下to_numpy属性访问基础numpy数组:

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