简体   繁体   English

如何将 dataframe 转换为 numpy 数组?

[英]How to convert dataframe into numpy array?

So I am currently making a neural network MLP (Multi-layer-Perceptron) on group classification on sea turtle speed between beach to sea on the seconds unit, and it looks somewhat like this.所以我目前正在制作一个神经网络 MLP (Multi-layer-Perceptron) 以秒为单位对海滩到大海之间的海龟速度进行分组分类,看起来有点像这样。

在此处输入图像描述

Now on my Jupyter Notebook I type the following现在在我的 Jupyter Notebook 上输入以下内容

data = pd.read_csv("SeaTurtles.csv")
data

It shows the data in dataframe它显示了 dataframe 中的数据

在此处输入图像描述

What I wanted to do is to separate these two groups which are group A for "Training" data and group B for "Testing" data.我想要做的是将这两组分开,即“培训”数据的 A 组和“测试”数据的 B 组。 I wanted to convert the dataframe into a NumPy array so that it would make it easier for me to classify them into the SpeciesCode.我想将 dataframe 转换为 NumPy 数组,以便我更容易将它们分类到 SpeciesCode 中。 So I type in:所以我输入:

newdf=data.drop(['Group1','Group 2'], axis = 1)
newdf

在此处输入图像描述

I need to split it into two test group A and B so I typed in我需要把它分成两个测试组 A 和 B 所以我输入

groupA=newdf.loc[newdf['Test*'] == 'A']
groupB=newdf.loc[newdf['Test*'] == 'B']

在此处输入图像描述

Group B B组在此处输入图像描述

What I did was to convert these into numpy array with.to_numpy()我所做的是将这些转换为 numpy 数组 with.to_numpy()

groupA = groupA.to_numpy()
groupA

and it returns the "AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy' "它返回“AttributeError:'numpy.ndarray' object has no attribute 'to_numpy'” 在此处输入图像描述

My question is did I do something wrong or is there another way for me to convert this dataframe into numpy array so that I can start training the data?我的问题是我做错了什么还是有另一种方法可以将这个 dataframe 转换为 numpy 数组,以便我可以开始训练数据? Thank you in advance.先感谢您。

Pandas dataframe is a two-dimensional data structure to store and retrieve data in rows and columns format. Pandas dataframe 是一种二维数据结构,用于以行和列格式存储和检索数据。

You can convert pandas dataframe to numpy array using the df.to_numpy() method.您可以使用 df.to_numpy() 方法将 pandas dataframe 转换为 numpy 数组。

You can use the below code snippet to convert pandas dataframe into numpy array.您可以使用以下代码片段将 pandas dataframe 转换为 numpy 数组。 numpy_array = df.to_numpy() print(type(numpy_array)) numpy_array = df.to_numpy() print(type(numpy_array))

Output: Output:

<class 'numpy.ndarray'> <类'numpy.ndarray'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM