pandas: complex filter on rows of DataFrame

后端 未结 6 1589
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 15:22

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(.         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 15:46

    Suppose I had a DataFrame as follows:

    In [39]: df
    Out[39]: 
          mass1     mass2  velocity
    0  1.461711 -0.404452  0.722502
    1 -2.169377  1.131037  0.232047
    2  0.009450 -0.868753  0.598470
    3  0.602463  0.299249  0.474564
    4 -0.675339 -0.816702  0.799289
    

    I can use sin and DataFrame.prod to create a boolean mask:

    In [40]: mask = (np.sin(df.velocity) / df.ix[:, 0:2].prod(axis=1)) > 0
    
    In [41]: mask
    Out[41]: 
    0    False
    1    False
    2    False
    3     True
    4     True
    

    Then use the mask to select from the DataFrame:

    In [42]: df[mask]
    Out[42]: 
          mass1     mass2  velocity
    3  0.602463  0.299249  0.474564
    4 -0.675339 -0.816702  0.799289
    

提交回复
热议问题