Mark Ginsburg
Mark Ginsburg

Reputation: 2269

More terse way to filter Pandas dataframe?

The following code works.

df1 =  df.loc[lambda df: df.myfield == 'abc' , ['myfield' ,  'field2', 'field3','field4']]
print df1.iloc[:60]

However I would just like to print out the selected columns of the first 60 entries of df matching my 'abc' condition in a single statement without this extra step of assigning to df1 then doing the 2nd iloc statement. I tried adding a 2nd condition in my lambda but could not get it to work.

Upvotes: 0

Views: 54

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96257

At the very least, you should be able to simplify this to:

print df.loc[df.myfield == 'abc', ['myfield', 'field2', 'field3', 'field4']].iloc[:60]

generally, I would do something like:

row_selection = df.myfield == 'abc'
col_selection = ['myfield', 'field2', 'field3', 'field4']
print df.loc[row_selection, col_selection].iloc[:60]

Upvotes: 2

Related Questions