2

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 and value_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?

Irfanuddin
  • 2,295
  • 1
  • 15
  • 29
TLanni
  • 330
  • 1
  • 4
  • 15
  • And 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? – ALollz Mar 13 '19 at 18:21
  • index_column will have 0 to n rows,where n is number of rows in dataframe.But they may not be in ascending order. – TLanni Mar 13 '19 at 18:25
  • 1) this has been asked before, and 2) please don't follow any of the answers below if you're using 0.24. – cs95 Mar 13 '19 at 18:31

3 Answers3

2
np_arr = df.value_column.values
Ashish kulkarni
  • 644
  • 2
  • 7
  • 13
2

Pandas internally uses np.arrays. The values attribute is all you need:

df.value_column.values

is the np.array that you want.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

np_arr = df.value_column.values

if your column name has special characters like space, use the following:

np_arr = df['temperature [Celsius]'].values

sourebh_s
  • 23
  • 6