pandas: complex filter on rows of DataFrame

后端 未结 6 1572
没有蜡笔的小新
没有蜡笔的小新 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:40

    Specify reduce=True to handle empty DataFrames as well.

    import pandas as pd
    
    t = pd.DataFrame(columns=['a', 'b'])
    t[t.apply(lambda x: x['a'] > 1, axis=1, reduce=True)]
    

    https://crosscompute.com/n/jAbsB6OIm6oCCJX9PBIbY5FECFKCClyV/-/apply-custom-filter-on-rows-of-dataframe

提交回复
热议问题