I have a dataframe, columns to be used are "sepal_length" and "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. .to_numpy() just turns the whole 2 columns into large-sized numpy array, so it does not work for me. How can I work out?
Asked
Active
Viewed 2,130 times
0

user14416085
- 61
- 5
-
does this solve your problem https://stackoverflow.com/questions/13187778/convert-pandas-dataframe-to-numpy-array – Joe Ferndz Oct 30 '20 at 06:35
-
Does this answer your question? [Mapping rows of a Pandas dataframe to numpy array](https://stackoverflow.com/questions/51468593/mapping-rows-of-a-pandas-dataframe-to-numpy-array) – Ruli Oct 30 '20 at 06:37
1 Answers
0
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. I hope the following code will help you:
point=[]
for idx, row in df.iterrows():
p=row.to_numpy()
point.append(p)
print(point)

Qamar Abbas
- 176
- 7