简体   繁体   English

通过使用 pandas 在循环中比较列和变量来为列分配值

[英]Assigning values to the column by comparing a column and a variable in loop using pandas

I have two columns, one with students names and other is just empty.我有两列,一列是学生姓名,另一列是空的。 I have a list containing the students names who have passed.我有一个包含已通过学生姓名的列表。 so in for loop, I will be taking each name from list and compare it with first column values.所以在 for 循环中,我将从列表中获取每个名称并将其与第一列值进行比较。 If name found in the column, then add "Pass" in the econd column corresponding to that name.如果在该列中找到名称,则在与该名称对应的第二列中添加“通过”。

Have to go through each value in list and fill the PASS/FAIL in the second column.必须通过列表中的每个值 go 并在第二列中填写 PASS/FAIL。 But the issue is, I dont know how to pass variable name in pandas condition.但问题是,我不知道如何在 pandas 条件下传递变量名。 if I pass the value of variable directly, it works.如果我直接传递变量的值,它就可以工作。 Could someone suggest, how to pass variable name instead of value directly.有人可以建议,如何直接传递变量名而不是值。

    df.loc[df['student_name'] == "John", 'status'] = "Pass"
    

I want to replace this name "John" with variable name (passed_students) from loop which will be like:我想用循环中的变量名(passed_students)替换这个名字“John”,就像:

      df.loc[df['student_name'] == passed_students, 'status'] = "Pass"

But it is throwing "KeyError: 'student_name'"但它正在抛出“KeyError:'student_name'”

Use the .isin() function to vectorize the operation.使用.isin() function 对操作进行矢量化。

import pandas as pd
import string
import numpy as np

## create dummy data
df = pd.DataFrame({"students":list(string.ascii_lowercase[:26]),"status":"Failed"})
passed_students = list(string.ascii_lowercase[:6])

print(df.head())
##look for students who have passed and assign "Passed" status.
df.loc[df.students.isin(passed_students),"status"] = "Passed"
print(df.head())

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

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