简体   繁体   English

将数据框行转换为 numpy 数组

[英]dataframe row as into numpy array

在此处输入图片说明

I have a dataframe, columns to be used are "sepal_length" and "sepal_width".我有一个数据框,要使用的列是“sepal_length”和“sepal_width”。 I want to turn each single row into a single data point, like point1= [5.1 3.5] and point2 = [4.9 3] and so on.我想把每一行变成一个单一的数据点,比如point1= [5.1 3.5]point2 = [4.9 3]等等。 .to_numpy() just turns the whole 2 columns into large-sized numpy array, so it does not work for me. .to_numpy() 只是将整个 2 列变成大型 numpy 数组,所以它对我不起作用。 How can I work out?我该如何锻炼?

You can simply use this:你可以简单地使用这个:

nump_df=df.values
print(nump_df)

or或者

You can iterate over rows and convert each row to numpy array and append those arrays in a list.您可以遍历行并将每一行转换为 numpy 数组并将这些数组附加到列表中。 I hope the following code will help you:我希望以下代码可以帮助您:

point=[]
for idx, row in df.iterrows():
    p=row.to_numpy()
    point.append(p)

print(point)

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

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