使用pandas的case语句

时间:2018-04-05 22:50:00

标签: python pandas

我需要输入来解决这个用例

    import pandas as pd
    data = {'name': ['A', 'B', 'C', 'D', 'E'], 
            'score': [1,2,3,4,5],
            'status': ['Active','Inactive','Active','Inactive','Active']
            }
    df = pd.DataFrame(data)

    #create a new column (check_flag) set to 1 where status = Active and score in ( 3 and 5) else 0
    df['check_flag']= 

    #keep the records where status = Active and score in ( 3 and 5)

由于

2 个答案:

答案 0 :(得分:1)

我希望以下解决方案有帮助

df['check_flag'] = ((df['status']== 'Active') & (df['score']>=3))

这将创建一个新列。

答案 1 :(得分:0)

谢谢Paras!

我在下面尝试了它也有效

df.loc [(df [' status'] ==' Active')& (df ['得分']。isin([3,5])),' checkflag'] = 1