I have one data frame & two (or multiple) lists of indexes:
JavaScript
x
8
1
data_ = {'Number_a': [12, 13, 14,15,16,17],'Number_b':[11,11,11,12,12,12],'Number_c': [10,5,4,3,2,1]}
2
3
data = pd.DataFrame(data=data_)
4
5
idx1= [0,2,4]
6
idx2=[1,3,5]
7
idx3 =[ , ,..]
8
I want to create a loop where I can select the rows of data
. for each iteration, I use one list.
so for 1st iteration data has the rows shown in idx1
0,2,4.
how I can do that ?
This is a simplified example, in my actual code, there are different functions that I need to loop on. for each iteration having different rows. Therefore it’s important for me to do that within a loop.
Advertisement
Answer
You can select rows with data.loc[rows]
.
JavaScript
1
9
1
data_ = {'Number_a': [12, 13, 14,15,16,17],'Number_b':[11,11,11,12,12,12],'Number_c': [10,5,4,3,2,1]}
2
3
data = pd.DataFrame(data=data_)
4
idx1 = [0,2,4]
5
idx2 = [1,3,5]
6
7
for r in (idx1, idx2):
8
print(data.loc[r])
9
Output:
JavaScript
1
9
1
Number_a Number_b Number_c
2
0 12 11 10
3
2 14 11 4
4
4 16 12 2
5
Number_a Number_b Number_c
6
1 13 11 5
7
3 15 12 3
8
5 17 12 1
9