Combination of values in pandas data frame

前端 未结 1 1140
野的像风
野的像风 2021-01-02 11:36

This is my pandas dataframe:

       Item          Support_Count
0      BREAD              4
1      MILK               4
2      DIAPER             4
3      BE         


        
相关标签:
1条回答
  • 2021-01-02 12:06

    You can use the itertools library:

    import itertools
    list(itertools.combinations(df['Item'], 2))
    
    [('BREAD', 'MILK'),
     ('BREAD', 'DIAPER'),
     ('BREAD', 'BEER'),
     ('MILK', 'DIAPER'),
     ('MILK', 'BEER'),
     ('DIAPER', 'BEER')]
    
    list(itertools.combinations(df['Item'], 3))
    
    [('BREAD', 'MILK', 'DIAPER'),
     ('BREAD', 'MILK', 'BEER'),
     ('BREAD', 'DIAPER', 'BEER'),
     ('MILK', 'DIAPER', 'BEER')]
    

    Note: The number of combinations grows very quickly so generating all possible combinations may not be efficient. I recommend looking at apriori algorithm implementations if you haven't already done so.

    0 讨论(0)
提交回复
热议问题