Mapping methods across multiple columns in a Pandas DataFrame

前端 未结 3 1853
挽巷
挽巷 2021-01-23 21:31

I have a Pandas dataframe where the values are lists:

import pandas as pd

DF = pd.DataFrame({\'X\':[[1, 5], [1, 2]], \'Y\':[[1, 2, 5], [1, 3, 5]]})
DF
                  


        
3条回答
  •  长情又很酷
    2021-01-23 21:54

    Use set and issubset:

    DF.assign(x_sub_y = DF.apply(lambda x: set(x.X).issubset(set(x.Y)), axis=1))
    

    Output:

            X          Y  x_sub_y
    0  [1, 5]  [1, 2, 5]     True
    1  [1, 2]  [1, 3, 5]    False
    

提交回复
热议问题