Python equivalent to subset function in r

968 views Asked by At

I don't know python at all but the project I'm currently working on must be done using it, I have this r code

y_train <- subset(train_df_cleaned_2, Id==IdExec)$Y

and I need to do the exact same thing in python. How do I do it? Thank you

2

There are 2 answers

0
Samwise On

The standard way of doing this in Python is comprehension, e.g.:

y_train = [x for x in train_df_cleaned_2 if x.Id == IdExec]
0
JareBear On

I found this answer using this :subsetting a Python DataFrame

y = train[(train.NflId == train.NflIdRusher)][['Yards']]

thanks for the help everyone