简体   繁体   English

有没有一种方法可以从pandas DataFrame的特定列中生成一个numpy数组?

[英]is there a way to generate a numpy array from specific columns of a pandas DataFrame?

this is constructing data 这是在构建数据

X = np.array([[1.   , 0.697, 0.46 ],
       [2.   , 0.774, 0.376],
       [3.   , 0.634, 0.264]])
df = pd.DataFrame(X, columns=list('ABC'))

numpy slicing numpy切片

X[:,1:]

outputs (snippet_1) 输出(snippet_1)

array([[0.697, 0.46 ],
       [0.774, 0.376],
       [0.634, 0.264]])

assume we start with df instead of X 假设我们以df而不是X开头

df.to_numpy()

outputs 输出

array([[1.   , 0.697, 0.46 ],
       [2.   , 0.774, 0.376],
       [3.   , 0.634, 0.264]])

is there a way to generate a numpy array from last 2 columns of a pandas DataFrame, like snippet_1? 有没有一种方法可以从snippet_1之类的pandas DataFrame的后两列生成一个numpy数组?

use iloc to choose columns 使用iloc选择列

df.iloc[:,1:].to_numpy()

array([[0.697, 0.46 ],
       [0.774, 0.376],
       [0.634, 0.264]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM