Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert pandas series into numpy array [duplicate]

I am new to pandas and python. My input data is like

category   text
1   hello iam fine. how are you
1   iam good. how are you doing.

inputData= pd.read_csv(Input', sep='\t', names=['category','text'])
X = inputData["text"]
Y = inputData["category"]

here Y is the panda series object, which i want to convert into numpy array. so i tried .as_matrix

YArray= Y.as_matrix(columns=None)
print YArray

But i got the output as [1,1] (which is wrong since i have only one column category and two rows). I want the result as 2x1 matrix.

like image 428
vishnu Avatar asked May 29 '17 09:05

vishnu


People also ask

How can you convert a series object to a NumPy array?

to_numpy() Pandas Series. to_numpy() function is used to return a NumPy ndarray representing the values in given Series or Index. This function will explain how we can convert the pandas Series to numpy Array.

Is pandas Series A NumPy array?

A numpy array is a grid of values that belong to the same data type. NumPy arrays are created using the array() function. A Pandas Series is a one-dimensional labeled array that can store data of any type. It is created using the Series() function of the Pandas library.

Can series be passed to NumPy functions?

Series can also be created using multi-dimensional Numpy arrays. However, after making the multi-dimensional array, first convert the array into a nested list, and then pass the list to the data parameter of the series constructor.


2 Answers

To get numpy array, you need

Y.values
like image 91
gzc Avatar answered Oct 07 '22 15:10

gzc


Try this:
after applying the .as_matrix on your series object

Y.reshape((2,1))

Since .as_matrix() only returns a numpy-array NOT a numpy-matrix. Link here

like image 15
chetan reddy Avatar answered Oct 07 '22 15:10

chetan reddy