Home  >  Article  >  Backend Development  >  How to traverse DataFrame by row in Python_python

How to traverse DataFrame by row in Python_python

不言
不言Original
2018-04-08 10:53:527013browse

The following article will share with you a method of traversing DataFrame by row in Python, which has a good reference value. I hope it will be helpful to everyone. Let’s take a look together

When making a classification model, you need to obtain data by row in the DataFrame for training and testing.

import pandas as pd
dict=[[1,2,3,4,5,6],[2,3,4,5,6,7],[3,4,5,6,7,8],[4,5,6,7,8,9],[5,6,7,8,9,10]]
data=pd.DataFrame(dict)
print(data)
for indexs in data.index:
 print(data.loc[indexs].values[0:-1])

Experimental results:

/usr/bin/python3.4 /home/ubuntu/PycharmProjects/pythonproject/findgaoxueya/test.py
 0 1 2 3 4 5
0 1 2 3 4 5 6
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
[1 2 3 4 5]
[2 3 4 5 6]
[3 4 5 6 7]
[4 5 6 7 8]
[5 6 7 8 9]
Process finished with exit code 0



The above is the detailed content of How to traverse DataFrame by row in Python_python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn