SoFunction
Updated on 2024-10-30

Traversing a DataFrame by Rows in Python

When doing classification modeling, you need to get data by rows in 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=(dict)
print(data)
for indexs in :
 print([indexs].values[0:-1])

Results:

/usr/bin/python3.4 /home/ubuntu/PycharmProjects/pythonproject/findgaoxueya/
 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 method of traversing DataFrame in Python according to rows is all that I have shared with you, and I hope it will give you a reference, and I hope you will support me more.