在Python中使用数据框元素遍历列表

时间:2019-12-03 10:43:51

标签: python pandas

我想遍历一个以数据框为元素的列表。

示例: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

当我访问其元素时,出现以下错误。 列表索引必须是整数,而不是DataFrame

我尝试了以下代码:

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())

我想访问列表中的每个数据框元素,并比较每行的得分值。如果它大于cut_off值,则应将其求和,并提供给我总行数> cut_off值。

预期输出: 负数集:截止值= 0,变体数= 8

谢谢

3 个答案:

答案 0 :(得分:1)

这应该可以的

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())

答案 1 :(得分:0)

您似乎希望i是列表ls的索引,而实际上它是元素本身。例如:

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

输出

one
two
three

同时

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

输出:

0: one
1: two
2: three

所以我认为enumerate是您想要的。

答案 2 :(得分:0)

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'])))

我希望这对您有帮助...