Mapping methods across multiple columns in a Pandas DataFrame

前端 未结 3 1850
挽巷
挽巷 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:39

    Or you can try set

    DF['x_sub_y']=DF.X+DF.Y
    DF['x_sub_y']=DF['x_sub_y'].apply(lambda x : list(set(x)))==DF.Y
    DF
    Out[691]: 
            X          Y  x_sub_y
    0  [1, 5]  [1, 2, 5]     True
    1  [1, 2]  [1, 3, 5]    False
    

提交回复
热议问题