Debbie
Debbie

Reputation: 969

Empty dataframe getting printed when there's no match

smer_prods is a dictionary whose every key holds a list as a value:

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'],
}

for each key in the dictionary we are looping through its list value. If any value in the list match with the ITEM NAME in the pandas dataframe, it should print both the dataframe and the list value in the dictionary. It works perfectly when a match occurs. Additionally it is printing an empty dataframe when there's no match. I want to prevent this. When there's no match, no need to store an empty row in the dataframe 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)

Output:

--------------------------------------------------------------------------------
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']
------------------------------------------------------------------------------

The csv for pandas dataframe looks like this: enter image description here

Upvotes: 1

Views: 43

Answers (1)

Nihal
Nihal

Reputation: 5334

use .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)

example:

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

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

output:

True
False

Upvotes: 1

Related Questions