Home > Software engineering >  How to filter rows by conditional value in Pandas? [duplicate]
How to filter rows by conditional value in Pandas? [duplicate]

Time:09-25

I was trying to filter and create a new n x p dataframe from a dataset using the first column. If I wanted to capture all the values in the first column that are < 0.02, and make a new data frame that contains all the rows that satisfy that condition (while obviously keeping the rest of the columns), how would I go about that? Here is my attempt:

df_new = df[:, 1] < 0.02
print(df_new)

It returns a 1D vector of booleans. How can I retain the rest of my rows and columns intact while just filtering by the first column? Thank you.

CodePudding user response:

You are actually pretty close, this should work though:

df_new = df[df.iloc[:,0] < 0.02]
  • Related