根据索引列表在循环中选择数据框的行
我有一个数据框架&索引的两个(或多个)列表:
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]}
data = pd.DataFrame(data=data_)
idx1= [0,2,4]
idx2=[1,3,5]
idx3 =[...,...,..]
我想创建一个循环,可以在其中选择data
的行。对于每次迭代,我使用一个列表。 因此,对于第一迭代,数据的行中显示了idx1
0,2,4中的行。
我该怎么做?
这是一个简化的示例,在我的实际代码中,我需要循环使用不同的功能。对于每个迭代都有不同的行。因此,对我来说,在循环中这样做很重要。
I have one data frame & two (or multiple) lists of indexes:
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]}
data = pd.DataFrame(data=data_)
idx1= [0,2,4]
idx2=[1,3,5]
idx3 =[...,...,..]
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
data.loc [行]
选择行。输出:
You can select rows with
data.loc[rows]
.Output: