Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 缺少值时所有可能组的自定义分组_Python_Pandas_Python 2.7_Dataframe - Fatal编程技术网

Python 缺少值时所有可能组的自定义分组

Python 缺少值时所有可能组的自定义分组,python,pandas,python-2.7,dataframe,Python,Pandas,Python 2.7,Dataframe,我有一本代表一系列产品的字典。我需要找到这些产品中的所有重复产品。如果产品具有相同的产品类型,颜色和尺寸->,则它们是重复的。如果没有问题,我可以很容易地按('product_type'、'color'、'size')进行分组:缺少一些值。现在我必须找到所有可能的产品组,它们之间可能是重复的这意味着某些元素可以出现在多个组中。 让我举例说明: import pandas as pd def main(): data= {'product_id': [1, 2, 3, 4, 5, 6,

我有一本代表一系列产品的字典。我需要找到这些产品中的所有重复产品。如果产品具有相同的
产品类型
颜色
尺寸
->,则它们是重复的。如果没有问题,我可以很容易地按('product_type'、'color'、'size')进行分组:缺少一些值。现在我必须找到所有可能的产品组,它们之间可能是重复的这意味着某些元素可以出现在多个组中。

让我举例说明:

import pandas as pd


def main():
    data= {'product_id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
         'product_type': ['shirt', 'shirt', 'shirt', 'shirt', 'shirt', 'hat', 'hat', 'hat', 'hat', 'hat', 'hat', ],
         'color': [None, None, None, 'red', 'blue', None, 'blue', 'blue', 'blue', 'red', 'red', ],
                       'size': [None, 's', 'xl', None, None, 's', None, 's', 'xl', None, 'xl', ],
                       }
    print(data)

if __name__ == '__main__':
    main()
对于此数据:

我需要这个结果-每个可能组的可能重复产品列表(仅取最大的超级组):

例如,让我们以id=1的“衬衫”为例 该产品没有颜色或尺码,因此他可以与衬衫2(尺码为“s”,但没有颜色)和衬衫4(颜色为“红色”,但没有尺码)一起出现在可能的“重复组”中。所以这三件衬衫(1,2,4)可能是相同颜色“红色”和尺寸“s”的复制品

我试图通过循环所有可能的缺失值组合来实现它,但它感觉是错误和复杂的


是否有办法获得所需的结果?

您可以创建所有可能的非
None
键,然后检查哪个项属于哪个键-关于
None
s:

data= {'product_id'  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
       'product_type': ['shirt', 'shirt', 'shirt', 'shirt', 'shirt', 'hat',
                        'hat', 'hat', 'hat', 'hat', 'hat', ],
       'color'       : [None, None, None, 'red', 'blue', None, 'blue', 
                        'blue', 'blue', 'red', 'red', ],
       'size'        : [None, 's', 'xl', None, None, 's', None, 's', 'xl', None, 'xl', ]}



from itertools import product

# create all keys without None in it     
p = product((t for t in set(data['product_type']) if t), 
            (c for c in set(data['color']) if c), 
            (s for s in set(data['size']) if s))

# create the things you have in stock
inventar = list( zip(data['product_id'],data['product_type'],data['color'],data['size']))
d = {}

# order things into its categories
for cat in p:
    d.setdefault(cat,set())  # uses a set to collect the IDs
    for item in inventar:
        TY, CO, SI = cat
        ID, TYPE, COLOR, SIZE = item

        # the (TYPE or TY) will substitute TY for any TYPE that is None etc.
        if (TYPE or TY)==TY and (COLOR or CO)==CO and (SIZE or SI)==SI:
            d[cat].add(ID)

print(d)
输出:

# category-key            id's that match
{('shirt', 'blue', 's') : {1, 2, 5}, 
 ('shirt', 'blue', 'xl'): {1, 3, 5}, 
 ('shirt', 'red', 's')  : {1, 2, 4}, 
 ('shirt', 'red', 'xl') : {1, 3, 4}, 
 ('hat', 'blue', 's')   : {8, 6, 7}, 
 ('hat', 'blue', 'xl')  : {9, 7}, 
 ('hat', 'red', 's')    : {10, 6},
 ('hat', 'red', 'xl')   : {10, 11}}
Doku:


您可以创建所有可能的非
None
键,然后检查哪个项属于哪个键-关于
None
s:

data= {'product_id'  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
       'product_type': ['shirt', 'shirt', 'shirt', 'shirt', 'shirt', 'hat',
                        'hat', 'hat', 'hat', 'hat', 'hat', ],
       'color'       : [None, None, None, 'red', 'blue', None, 'blue', 
                        'blue', 'blue', 'red', 'red', ],
       'size'        : [None, 's', 'xl', None, None, 's', None, 's', 'xl', None, 'xl', ]}



from itertools import product

# create all keys without None in it     
p = product((t for t in set(data['product_type']) if t), 
            (c for c in set(data['color']) if c), 
            (s for s in set(data['size']) if s))

# create the things you have in stock
inventar = list( zip(data['product_id'],data['product_type'],data['color'],data['size']))
d = {}

# order things into its categories
for cat in p:
    d.setdefault(cat,set())  # uses a set to collect the IDs
    for item in inventar:
        TY, CO, SI = cat
        ID, TYPE, COLOR, SIZE = item

        # the (TYPE or TY) will substitute TY for any TYPE that is None etc.
        if (TYPE or TY)==TY and (COLOR or CO)==CO and (SIZE or SI)==SI:
            d[cat].add(ID)

print(d)
输出:

# category-key            id's that match
{('shirt', 'blue', 's') : {1, 2, 5}, 
 ('shirt', 'blue', 'xl'): {1, 3, 5}, 
 ('shirt', 'red', 's')  : {1, 2, 4}, 
 ('shirt', 'red', 'xl') : {1, 3, 4}, 
 ('hat', 'blue', 's')   : {8, 6, 7}, 
 ('hat', 'blue', 'xl')  : {9, 7}, 
 ('hat', 'red', 's')    : {10, 6},
 ('hat', 'red', 'xl')   : {10, 11}}
Doku: