56

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.

1
  • 2
    .values wil be shunned in favour of two new methods starting v0.24.0. See this answer. Commented Jan 23, 2019 at 10:09

3 Answers 3

90

To get numpy array, you need

Y.values
Sign up to request clarification or add additional context in comments.

4 Comments

when i do Y.values(), it throws, TypeError: 'numpy.ndarray' object is not callable
@vishnu It's a property, not a method.
thanks. but reshape worked for me!
@vishnu Not sure the original post is updated based on your comment, but the way is to do Y.values, not Y.values().
15

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

1 Comment

reshape is deprecated use Y.values.reshape(2,1) instead.
13

If df is your dataframe, then a column of the dataframe is a series and to convert it into an array,

df = pd.DataFrame()
x = df.values
print(x.type)

The following prints,

<class 'numpy.ndarray'>

successfully converting it to an array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.