pandas pandas数据框中的值组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36242735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combination of values in pandas data frame
提问by vikky
This is my pandas dataframe:
这是我的Pandas数据框:
Item Support_Count
0 BREAD 4
1 MILK 4
2 DIAPER 4
3 BEER 3
How will i generate all possible unique combinations of 2 and 3 set of items from the 1st column 'Item'.
我将如何从第一列“项目”中生成 2 组和 3 组项目的所有可能的独特组合。
Example(2 item sets): (BREAD,MILK) ,(BREAD,DIAPER),(BREAD,BEER),(MILK,DIAPER) etc.
示例(2个项目集):(面包,牛奶),(面包,尿布),(面包,啤酒),(牛奶,尿布)等。
Example (3 item sets): (BREAD,MILK,DIAPER),(BREAD,MILK,BEER),(MILK,DIAPER,BEER) etc.
示例(3 个项目集):(BREAD,MILK,DIAPER),(BREAD,MILK,BEER),(MILK,DIAPER,BEER) 等。
回答by ayhan
You can use the itertools
library:
您可以使用该itertools
库:
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 algorithmimplementations if you haven't already done so.
注意:组合的数量增长非常快,因此生成所有可能的组合可能效率不高。如果您还没有这样做,我建议您查看apriori 算法实现。