简体   繁体   English

在python中使用数据框元素循环遍历列表

[英]Looping through list with dataframe elements in python

I want to iterate over a list, which has dataframes as its elements.我想遍历一个列表,该列表的元素是数据框。

Example: ls is my list with below elements (two dataframes)示例:ls 是我的列表,包含以下元素(两个数据框)

                           seq  score    status
4366  CGAGGCTGCCTGTTTTCTAGTTG   5.15  negative
5837  GGACCTTTTTTACAATATAGCCA   3.48  negative
96    TTTCTAGCCTACCAAAATCGGAG  -5.27  negative
1369  CTTCCTATCTTCATTCTTCGACT   1.28  negative
1223                CAAGTTTGT   2.06  negative
5451  TGTTTCCACACCTGTCTCAGCTC   4.48  negative
1277  GTACTGTGGAATCTCGGCAGGCT   4.87  negative
5299  CATAATGAATGCCCCATCAATTG  -7.19  negative
3477                ATGGCACTG  -3.60  negative
2953  AGTAATTCTGTTGCCTGAAGATA   2.86  negative
4586                TGGGCAAGT   2.48  negative
3746                AATGAGAGG  -3.67  negative,
                         seq  score    status
1983  AGCAGATCAAACGGGTAAAGGAC  -4.81  negative
3822  CCCTGGCCCACGCACTGCAGTCA   3.32  negative
1127  GCAGAGATGCTGATCTTCACGTC  -6.77  negative
3624                TGAGTATGG   0.60  negative
4559                AAGGTTGGG   4.94  negative
4391  ATGAAGATCATCGAAATCAGTTT  -2.09  negative
4028  TCTCCGACAATGCCTATCAGTAC   1.14  negative
2694                CAGGGAACT   0.98  negative
2197  CTTCCATTGAGCTGCTCCAGCAC  -0.97  negative
2025  TGTGATCTGGCTGCACGCACTGT  -2.13  negative
5575                CCAGAAAGG  -2.45  negative
275   TCTGTTGGGTTTTCATACAGCTA   7.11  negative

When I am accessing its elements, I am getting following error.当我访问其元素时,出现以下错误。 list indices must be integers, not DataFrame列表索引必须是整数,而不是 DataFrame

I tried the following code:我尝试了以下代码:

cut_off = [1,2,3,4]

for i in ls:
    for co in cut_off:
        print "Negative set : " + "cut off value =", str(
            co), number of variants = ", str((ls[i]['score'] > co).sum())

I want to access each dataframe element in the list and compare the score value of each row.我想访问列表中的每个数据框元素并比较每行的分数值。 If it is more than the cut_off value, it should sum it and give me the total number of rows which value > cut_off value.如果它大于 cut_off 值,它应该对它求和并给我值 > cut_off 值的总行数。

Expected output: Negative set : cut off value = 0 , number of variants = 8预期输出:负集:截止值 = 0,变体数量 = 8

Thanks谢谢

This should work ok这应该可以正常工作

cut_off = [1,2,3,4]

for df in ls:
    for co in cut_off:
        print "Negative set : " + "cut off value =", str(
            co), number of variants = ", str((df['score'] > co).sum())

It looks like you are expecting i to be an index into your list ls , when in fact it is the element itself.看起来您希望i成为您的列表ls的索引,而实际上它是元素本身。 For example:例如:

foo = [ "one", "two", "three" ]
for i in foo:
     print(i)

outputs产出

one
two
three

while尽管

for i, elm in enumerate(foo):
     print(f"{i}: {elm}")

outputs:输出:

0: one
1: two
2: three

So I think enumerate is what you're looking for.所以我认为enumerate就是你要找的。

for i in range(len(ls)):
    for co in cut_off:
        print("Negative set : " + "cut off value =", str(
        co), number of variants = ", (sum(list((ls[ls['score'] > co]['score'])))

I hope this helps...我希望这有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM