没有匹配项时将打印空数据框

时间:2019-03-18 11:17:21

标签: python pandas dataframe

smer_prods是一本字典,其每个键都包含一个列表作为值:

smer_prods = {
    'ragi vermicelli' : ['ragi vermicelli'],
    'rice vermicelli' : ['rice vermicelli'],
    'vermicelli jupiter' : ['vermicelli jupiter'],
    'lemon & tamarind vermicelli' : ['lemon & tamarind vermicelli'],
    'finosta vermicelli' : ['finosta vermicelli-5kg'],
    'rosted vermicelli' : ['roasted vermicelli'],
    'semiya/vermicelli' : ['semiya / vermicelli 900grams'],
    'red chili' : ['red chilli (lal mirch)','guntur red chilli','red chilly whole(lal mirch)', 'red chilly wg', 'red chilli whole (hot) 1 kg', 'red chilli whole (rich colour) 1 kg'],
    'red chili powder' : ['red chilli fresh-kg','red chilli powder (rich colour) 1 kg','red chilli powder (hot) 1 kg','red chilli powder','lal mirch powder','lal mirch powder 100gms', 'lal mirch powder 1kg', 'lal mirch powder 200gms', 'lal mirch powder 500gms'],
    'red chilli sauce' : ['red chilli sauce', 'red chilli sauce 200gm pet bottle 48X200gm', 'hot chili sauce'],
    'sriraja hot chilli sauce' : ['sriraja hot chilli sauce', 'sriracha hot chilli sauce'],
    'mineral water' : ['himalayan orchard pure peach flavoured natural mineral water - 500 ml','himalayan orchard pure strawberry flavoured natural mineral water - 500 ml','himalayan orchard pure apple flavoured natural mineral water - 500 ml','himalayan - the natural mineral water - 500ml bottle', 'himalayan - the natural mineral water - 200ml bottle', 'himalayan - the natural mineral water - 1ltr bottle'],
}

对于字典中的每个键,我们都在遍历其列表值。如果列表中的任何值与pandas数据框中的ITEM NAME匹配,则应在字典中同时打印数据框和列表值。发生匹配时,它可以完美工作。 此外,如果没有匹配项,它将打印一个空的数据框。我想防止这种情况。如果没有匹配项,则无需在数据帧df1中存储空行。

df = pd.read_csv('toy_data.csv', engine='python')
#print(df)
for x in smer_prods:
    list_smer = smer_prods[x]
    #print(list_smer)
    for y in list_smer:
        mask = df['ITEM NAME'] == y
        df1 = df[mask]
        print('-'*80)
        print(df1)
        print('-'*80)
        print(list_smer)

输出:

--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['vermicelli jupiter']
--------------------------------------------------------------------------------
   S.NO                 ITEM NAME
1     2  sriraja hot chilli sauce
--------------------------------------------------------------------------------
['sriraja hot chilli sauce', 'sriracha hot chilli sauce']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['sriraja hot chilli sauce', 'sriracha hot chilli sauce']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['himalayan orchard pure peach flavoured natural mineral water - 500 ml', 'himalayan orchard pure strawberry flavoured natural mineral water - 500 ml', 'himalayan orchard pure apple flavoured natural mineral water - 500 ml', 'himalayan - the natural mineral water - 500ml bottle', 'himalayan - the natural mineral water - 200ml bottle', 'himalayan - the natural mineral water - 1ltr bottle']
--------------------------------------------------------------------------------
   S.NO                                          ITEM NAME
8     9  himalayan orchard pure strawberry flavoured na...
--------------------------------------------------------------------------------
['himalayan orchard pure peach flavoured natural mineral water - 500 ml', 'himalayan orchard pure strawberry flavoured natural mineral water - 500 ml', 'himalayan orchard pure apple flavoured natural mineral water - 500 ml', 'himalayan - the natural mineral water - 500ml bottle', 'himalayan - the natural mineral water - 200ml bottle', 'himalayan - the natural mineral water - 1ltr bottle']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['himalayan orchard pure peach flavoured natural mineral water - 500 ml', 'himalayan orchard pure strawberry flavoured natural mineral water - 500 ml', 'himalayan orchard pure apple flavoured natural mineral water - 500 ml', 'himalayan - the natural mineral water - 500ml bottle', 'himalayan - the natural mineral water - 200ml bottle', 'himalayan - the natural mineral water - 1ltr bottle']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['himalayan orchard pure peach flavoured natural mineral water - 500 ml', 'himalayan orchard pure strawberry flavoured natural mineral water - 500 ml', 'himalayan orchard pure apple flavoured natural mineral water - 500 ml', 'himalayan - the natural mineral water - 500ml bottle', 'himalayan - the natural mineral water - 200ml bottle', 'himalayan - the natural mineral water - 1ltr bottle']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['himalayan orchard pure peach flavoured natural mineral water - 500 ml', 'himalayan orchard pure strawberry flavoured natural mineral water - 500 ml', 'himalayan orchard pure apple flavoured natural mineral water - 500 ml', 'himalayan - the natural mineral water - 500ml bottle', 'himalayan - the natural mineral water - 200ml bottle', 'himalayan - the natural mineral water - 1ltr bottle']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['himalayan orchard pure peach flavoured natural mineral water - 500 ml', 'himalayan orchard pure strawberry flavoured natural mineral water - 500 ml', 'himalayan orchard pure apple flavoured natural mineral water - 500 ml', 'himalayan - the natural mineral water - 500ml bottle', 'himalayan - the natural mineral water - 200ml bottle', 'himalayan - the natural mineral water - 1ltr bottle']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['vermicelli upma']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['roasted vermicelli']
--------------------------------------------------------------------------------
   S.NO        ITEM NAME
4     5  rice vermicelli
--------------------------------------------------------------------------------
['rice vermicelli']
--------------------------------------------------------------------------------
   S.NO               ITEM NAME
0     1  finosta vermicelli-5kg
--------------------------------------------------------------------------------
['finosta vermicelli-5kg']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['red chilli sauce', 'red chilli sauce 200gm pet bottle 48X200gm', 'hot chili sauce']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['red chilli sauce', 'red chilli sauce 200gm pet bottle 48X200gm', 'hot chili sauce']
--------------------------------------------------------------------------------
   S.NO        ITEM NAME
2     3  hot chili sauce
--------------------------------------------------------------------------------
['red chilli sauce', 'red chilli sauce 200gm pet bottle 48X200gm', 'hot chili sauce']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['lemon & tamarind vermicelli']
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [S.NO, ITEM NAME]
Index: []
--------------------------------------------------------------------------------
['red chilli (lal mirch)', 'guntur red chilli', 'red chilly whole(lal mirch)', 'red chilly wg', 'red chilli whole (hot) 1 kg', 'red chilli whole (rich colour) 1 kg']
--------------------------------------------------------------------------------
   S.NO          ITEM NAME
3     4  guntur red chilli
--------------------------------------------------------------------------------
['red chilli (lal mirch)', 'guntur red chilli', 'red chilly whole(lal mirch)', 'red chilly wg', 'red chilli whole (hot) 1 kg', 'red chilli whole (rich colour) 1 kg']
------------------------------------------------------------------------------

csv for pandas数据框如下所示: enter image description here

1 个答案:

答案 0 :(得分:1)

使用.any()

for x in smer_prods:
    list_smer = smer_prods[x]
    #print(list_smer)
    for y in list_smer:
        mask = df['ITEM NAME'] == y
        if mask.any() == True:
            df1 = df[mask]
            print('-'*80)
            print(df1)
            print('-'*80)
            print(list_smer)

示例:

mask = [False, True, False]
print(any(mask))

mask = [False, False, False]
print(any(mask))

输出:

True
False