Converting Pandas Dataframe to numpy array
12,761
Solution 1
Pandas internally uses np.arrays
. The values
attribute is all you need:
df.value_column.values
is the np.array
that you want.
Solution 2
np_arr = df.value_column.values
Solution 3
np_arr = df.value_column.values
if your column name has special characters like space, use the following:
np_arr = df['temperature [Celsius]'].values
Author by
TLanni
Updated on December 10, 2022Comments
-
TLanni over 1 year
The following is a Pandas dataframe
df
index_column value_column 0 20 2 28 1 30
It needs to be converted into a numpy array where
index_column
becomes the index of numpy array andvalue_column
becomes the corresponding value.That is
np_arr[0]=20
,np_arr[1]=30
,np_arr[2]=28
and so on.How can this be achieved?
-
ALollz about 5 yearsAnd what happens when
index_column
isn't a RangeIndex starting from 0?numpy
array indexing is 0 based, so do you need this to work in general for any arbitrary index arrangement? -
TLanni about 5 yearsindex_column will have 0 to n rows,where n is number of rows in dataframe.But they may not be in ascending order.
-
cs95 about 5 years1) this has been asked before, and 2) please don't follow any of the answers below if you're using 0.24.
-
-
Ashish kulkarni about 5 years@amandasmith: What you have mentioned is a good way to do it.