pythonpandasdataframedata-munging

pandas dataframe get rows when list values in specific columns meet certain condition


I have a dataframe:

df = A      B 
     1  [0.2,0.8]
     2  [0.6,0.9]

I want to get only rows where all the values of B are >= 0.5 So here:

new_df = A     B
         2 [0.6, 0.9]

What is the best way to do it?


Solution

  • You can use apply to filter the values,

    import pandas as pd
    df = pd.DataFrame({'A': [1,2], 'B':[[0.2, 0.8], [0.6, 0.9]]})
    print(df[df['B'].apply(lambda x: all([i>=0.5 for i in x]))])