python - 没有匹配时打印空数据框

标签 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 数据框中的项目名称匹配,则它应该打印数据框和字典中的列表值。当比赛发生时它可以完美地工作。 此外,当没有匹配时,它会打印一个空数据框。我想阻止这种情况。当没有匹配项时,无需在数据帧 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']
------------------------------------------------------------------------------

pandas 数据框的 csv 看起来像这样: enter image description here

最佳答案

使用.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

关于python - 没有匹配时打印空数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55220148/

相关文章:

python - 如何找到一个日期和另一个日期之间的差异(如果第二个未完全插入到dataFrame中)

r - 按名称排列的每一行最近 21 天的事件

python从特定行位置的数据帧收集数据

python - pd.qcut 的值为 inf(无穷大) ValueError : Bin edges must be unique:

python - 如何合并多个 Pandas 数据框列

Python 以聚合方式操作时间序列数据

python - 创建具有条件的多列

c# - 更改 deedle 框架中的列顺序

python - 将 pylab 与 ipython 一起使用 - ValueError : unknown locale: UTF-8

python - 如何用新数据更新 SVM 模型