Pandas 数据框打印额外信息

smer_prods 是一个看起来像这样的字典:


smer_prods = {

    #'vermicelli' : ['vermicelli', 'ragi vermicelli', 'rice vermicelli'],

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

    'vermicelli upma' : ['vermicelli upma'],

    'vermicelli payasam mix' : ['vermicelli payasam mix'],

    'mung bean vermicelli' : ['mung bean vermicelli'],

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

}

数据帧 df 的 csv 是这样的:

http://img.mukewang.com/61b1adb700014e9c04950190.jpg

现在当我执行这个简单的代码时:


for x in smer_prods:

    mask = df['ITEM NAME'] == x

    df1 = df[mask]

    print(df1['ITEM NAME'])


慕标5832272
浏览 249回答 1
1回答

蓝山帝景

我认为你需要列表中的测试值,所以使用列表理解和isin字典的测试值:df = pd.DataFrame({'ITEM NAME':['ragi vermicelli','red chilli (lal mirch)']})print (df)                ITEM NAME0         ragi vermicelli1  red chilli (lal mirch)#if need matched valuesL = [y for k, v in smer_prods.items()        for y in df.loc[df['ITEM NAME'].isin(v), 'ITEM NAME'].tolist()]print (L)['ragi vermicelli', 'red chilli (lal mirch)']#if need matched key of dictionaryL1 = [k for k, v in smer_prods.items()         for y in df.loc[df['ITEM NAME'].isin(v), 'ITEM NAME'].tolist()]print (L1)['ragi vermicelli', 'red chili']循环解决方案:L = []for k, v in smer_prods.items():    for y in df.loc[df['ITEM NAME'].isin(v), 'ITEM NAME'].tolist():        print (y)        L.extend([y])        #if need matched key of dictionary        #L.extend([k])print (L)['ragi vermicelli', 'red chilli (lal mirch)']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python