gpt4 book ai didi

python - Pandas 数据框打印额外信息

转载 作者:行者123 更新时间:2023-12-01 08:11:17 25 4
gpt4 key购买 nike

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 是这样的: enter image description here

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

for x in smer_prods:
mask = df['ITEM NAME'] == x
df1 = df[mask]
print(df1['ITEM NAME'])

它应该只打印 ITEM_NAME 列,但它打印了很多不必要的额外信息:

4    rice vermicelli
Name: ITEM NAME, dtype: object
Series([], Name: ITEM NAME, dtype: object)
Series([], Name: ITEM NAME, dtype: object)
5 ragi vermicelli
Name: ITEM NAME, dtype: object
Series([], Name: ITEM NAME, dtype: object)
Series([], Name: ITEM NAME, dtype: object)
Series([], Name: ITEM NAME, dtype: object)
Series([], Name: ITEM NAME, dtype: object)
Series([], Name: ITEM NAME, dtype: object)
Series([], Name: ITEM NAME, dtype: object)
Series([], Name: ITEM NAME, dtype: object)
Series([], Name: ITEM NAME, dtype: object)
1 sriraja hot chilli sauce
Name: ITEM NAME, dtype: object
Series([], Name: ITEM NAME, dtype: object)
Series([], Name: ITEM NAME, dtype: object)

我只想要列表中匹配的项目。我为此编写了代码:

for x in smer_prods:
mask = df['ITEM NAME'] == x
df1 = df[mask]
item_name = df1['ITEM NAME'].tolist()
print(item_name)

它产生了这样的输出:['米粉']

[]
[]
['ragi vermicelli']
[]
[]
[]
[]
[]
[]
[]
[]
['sriraja hot chilli sauce']
[]
[]

期望的输出:

['rice vermicelli', 'ragi vermicelli', 'sriraja hot chili sauce']

编辑:如果我这样写:

match = []
for x in smer_prods:
mask = df['ITEM NAME'] == x
if mask.any() == True:
df1 = df[mask]

item_name = df1['ITEM NAME']#note
match.append(item_name)
print(match)
print('-'*80)

它输出:

[4    rice vermicelli
Name: ITEM NAME, dtype: object, 5 ragi vermicelli
Name: ITEM NAME, dtype: object, 1 sriraja hot chilli sauce
Name: ITEM NAME, dtype: object]

最佳答案

我认为您需要列表中的测试值,因此请使用带有 isin 的列表理解来测试字典的值:

df = pd.DataFrame({'ITEM NAME':['ragi vermicelli','red chilli (lal mirch)']})
print (df)
ITEM NAME
0 ragi vermicelli
1 red chilli (lal mirch)

#if need matched values
L = [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 dictionary
L1 = [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)']

关于python - Pandas 数据框打印额外信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55236211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com