简体   繁体   中英

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

X[:,1:]

outputs (snippet_1)

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

assume we start with df instead of 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?

use iloc to choose columns

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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