pandas: complex filter on rows of DataFrame

Posted by duckworthd on Stack Overflow See other posts from Stack Overflow or by duckworthd
Published on 2012-07-10T16:56:37Z Indexed on 2012/07/10 21:15 UTC
Read the original article Hit count: 270

Filed under:

I would like to filter rows by a function of each row, e.g.

def f(row):
  return sin(row['velocity'])/np.prod(['masses']) > 5

df = pandas.DataFrame(...)
filtered = df[apply_to_all_rows(df, f)]

Or for another more complex, contrived example,

def g(row):
  if row['col1'].method1() == 1:
    val = row['col1'].method2() / row['col1'].method3(row['col3'], row['col4'])
  else:
    val = row['col2'].method5(row['col6'])
  return np.sin(val)

df = pandas.DataFrame(...)
filtered = df[apply_to_all_rows(df, g)]

How can I do so?

© Stack Overflow or respective owner

Related posts about pandas