Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert pandas selected columns into a 2D numpy array

I have a pandas dataframe with 10 columns and N rows. I want to select two columns from my data frame and put them into a NumPy 2D array with dimensions (N, 2). How to do that?

like image 234
user12394113 Avatar asked Feb 02 '26 21:02

user12394113


1 Answers

There is a way to convert a dataframe to a numpy array: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html

You could just select the columns you want:

df[['Col1','Col2']].to_numpy()
like image 127
formicaman Avatar answered Feb 05 '26 10:02

formicaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!