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 13 daysThe following is a Pandas dataframe
dfindex_column value_column 0 20 2 28 1 30It needs to be converted into a numpy array where
index_columnbecomes the index of numpy array andvalue_columnbecomes the corresponding value.That is
np_arr[0]=20,np_arr[1]=30,np_arr[2]=28and so on.How can this be achieved?
-
ALollz almost 4 yearsAnd what happens whenindex_columnisn't a RangeIndex starting from 0?numpyarray indexing is 0 based, so do you need this to work in general for any arbitrary index arrangement? -
TLanni almost 4 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 almost 4 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 almost 4 years@amandasmith: What you have mentioned is a good way to do it.