根据y_train值将X_train分为两个数据帧

时间:2020-08-19 00:07:03

标签: pandas

我具有以下函数,该函数查看y_train的每一行(单列)的值,该值为0或1,并基于此函数将每个对应的X_train行放入Tmaj或Tmin。但是我没有正确建立索引,或者也许有更好的方法。

def fun(X_train,y_train):
    Tmaj = pd.DataFrame()
    Tmin = pd.DataFrame()

    row=0
    for each in y_train['Outcome']:
        if each==1:
            Tmaj.append(X_train.loc[[row]])
        #else:
            #Tmin.append(X_train.loc[[]])
        row+=1 

1 个答案:

答案 0 :(得分:1)

代替循环,只用布尔掩码索引X_train

def fun(X_train, y_train):
    Tmaj = X_train.loc[y_train.Outcome==1, :]
    Tmin = X_train.loc[y_train.Outcome==0, :]
    return Tmaj, Tmin
相关问题