使用pandas数据帧遍历数百万条记录

2024-09-30 10:37:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据框,如下图所示 Sample dataset image attached

根据课程代码栏,我必须给出结果。我的代码对于一小部分数据(10000)运行良好。但当我运行400-500万条记录时,结果永远不会回来

我正在遍历数据帧的每一行,并根据课程代码应用结果逻辑。 我不确定是否有一种有效的方法来实现同样的目标

下面是我的代码片段:

    for index, row in df_report.iterrows():
    if AB in row['Course code'][0]:
        if 100 in row['Course code'][0].values():
            row['CoursePrecedence']=list(row['Course code'][0].keys())[0]
            row['Result']='AB code has Grade A'
            df_var = df_var.append(row, ignore_index=True)
        else:
            row['CoursePrecedence']=list(row['Course code'][0].keys())[0]
            row['Result']='Pass with lower grade'
            df_var = df_var.append(row, ignore_index=True)

    elif AC in row['Course code'][0]:
        if 100 in row['Course code'][0].values():
              row['CoursePrecedence']=list(row['Course code'][0].keys())[0]
              row['Result']='A in all subjects'
              df_var = df_var.append(row, ignore_index=True)
        else:
              row['CoursePrecedence']=list(row['Course code'][0].keys())[0]
              row['Result']='something'
              df_var = df_var.append(row, ignore_index=True)
    elif AA in row['Course code'][0]:
        if 100 in row['Course code'][0].values():
              row['CoursePrecedence']=list(row['Course code'][0].keys())[0]
              row['Result']='Pass with Grade A'

              df_var = df_var.append(row, ignore_index=True)
        else:
            row['CoursePrecedence']=list(row['Course code'][0].keys())[0]
            row['Result']='Student did not show up for the exam'

            df_var = df_var.append(row, ignore_index=True)
    else:
          row['Result']='ERROR'
          row['CoursePrecedence']=row['Course code'][0]
          df_var = df_var.append(row, ignore_index=True)


    student Course code
    JohnD   [{AA:100}]
    JohnB   [{AA:100},{AA:100}]
    Tom         [{AA:100}]
    Matt    [{AC:100},{AB:100}]
    Susan   [{AC:100},{AB:100},{21120:100}]**strong text**

    student Course code          Result                CoursePrecedence
    JohnD   [{AA:100}]          Pass with Grade A         AA
    JohnB   [{AA:100},{AA:100}] AB code has Grade A       AB
    Tom     [{AA:100}]          Pass with Grade A         AA
    Matt    [{AC:100},{AB:100}] A in all subject          AC
    Susan   [{AC:100},{AB:100},{21120:100}] A in all subject    AC
--Any help will be much appreciated 

Tags: intruedfindexabvarcoderesult

热门问题