pandas 将熊猫系列转换为 numpy 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44238796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert pandas series into numpy array
提问by vishnu
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
这里 Y 是熊猫系列对象,我想将其转换为 numpy 数组。所以我试过 .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,1] (这是错误的,因为我只有一个列类别和两行)。我想要结果为 2x1 矩阵。
采纳答案by chetan reddy
回答by gzc
To get numpy array, you need
要获得 numpy 数组,您需要
Y.values
回答by Akshaya Natarajan
If df is your dataframe, then a column of the dataframe is a series and to convert it into an array,
如果 df 是您的数据框,则数据框的一列是一个系列并将其转换为数组,
df = pd.DataFrame()
x = df.values
print(x.type)
The following prints,
以下印记,
<class 'numpy.ndarray'>
successfully converting it to an array.
成功将其转换为数组。