Pandas filter data frame rows by function

后端 未结 1 881
生来不讨喜
生来不讨喜 2020-12-30 03:00

I want to filter a data frame by more complex function based on different values in the row.

Is there a possibility to filter DF rows by a boolean function like you

相关标签:
1条回答
  • 2020-12-30 03:41

    I think function is here not necessary, better and mainly faster is use boolean indexing:

    m = (df['Name'] == 'Alisa') & (df['Age'] > 24)
    print(m)
    0      True
    1     False
    2     False
    3     False
    4     False
    5     False
    6      True
    7     False
    8     False
    9     False
    10    False
    11    False
    dtype: bool
    
    #invert mask by ~
    df1 = df[~m]
    

    Function solution - is necessary return boolean only, better if some complicated filtering - need return for each row boolean only:

    def filter_fn(row):
        if row['Name'] == 'Alisa' and row['Age'] > 24:
            return False
        else:
            return True
    
    df = pd.DataFrame(d, columns=['Name', 'Age', 'Score'])
    m = df.apply(filter_fn, axis=1)
    print(m)
    0     False
    1      True
    2      True
    3      True
    4      True
    5      True
    6     False
    7      True
    8      True
    9      True
    10     True
    11     True
    dtype: bool
    
    df1 = df[m]
    
    0 讨论(0)
提交回复
热议问题