如何提取 pandas 系列元素并将其与数据框列中的行进行比较
how to extract pandas series element and compare it with rows in dataframe's column
我有以下数据框..
coupon_type dish_id dish_name dish_price dish_quantity
0 Rs 20 off 012 Sandwich 65 2
1 Rs 20 off 013 Chicken 125 3
2 Rs 20 off 013 Chicken 125 3
3 Rs 20 off 013 Chicken 125 3
ratings reviews coupon_type user_id order_id meals order_area
4 blah blah blah Rs 20 off 9 9 5 London
4 blah blah blah Rs 20 off 9 9 5 London
3 blah blah blah Rs 20 off 9 9 5 London
4 blah blah blah Rs 20 off 9 9 5 London
我正在 dish_name 列上进行分组。
df_dish_name = df_final.groupby('dish_name')
然后我在groupby上做一些ratio操作
这给了我以下 pandas 系列..我存储在 dish_specific_perf
dish_name
Chicken 45.000000
Sandwich 61.111111
然后我在 if 循环中检查一个条件..
if((dish_specific_perf < 50).any() == True):
如果条件为真,我想将 ("NP") 字符串添加到数据框中相应的菜名中。因此,在数据框中应该如下所示。
coupon_type dish_id dish_name dish_price dish_quantity
0 Rs 20 off 012 Sandwich 65 2
1 Rs 20 off 013 Chicken 125 3
2 Rs 20 off 013 Chicken 125 3
3 Rs 20 off 013 Chicken 125 3
ratings reviews coupon_type user_id order_id meals order_area
4 blah blah blah Rs 20 off 9 9 5 London
4 blah blah blah Rs 20 off 9 9 5 London
3 blah blah blah Rs 20 off 9 9 5 London
4 blah blah blah Rs 20 off 9 9 5 London
Flag
Null
NP
NP
NP
问题是我如何将系列元素与数据框 dish_name 列进行比较以检查鸡是否存在?
当我
dish_specific_perf[0]
它只是给了我一个数字 45。
请帮忙..
本质上,您正在查找我们可以在布尔系列上使用 map
的查找,因此以下内容将添加一个布尔标志:
df_final['Flag'] = df_final['dish_name'].map(dish_specific_perf < 50)
这通过根据系列索引查找 df 值并返回该值来实现。
然后您可以将布尔值转换为您想要的标志:
df_final['Flag'] = np.where(df_final['Flag'], 'NP', 'Null')
首先,您的 if
陈述不符合您的需要。我会像这样在组中循环完成整个事情:
for name, group in df_dish_name:
# Whatever awesome thing you are doing, which give you the ratio...
if ratio < 50:
df_final.loc[df_final['dish_name']==name, 'Flag'] = 'NP'
这将避免多次索引和选择,并且更易于维护。
我有以下数据框..
coupon_type dish_id dish_name dish_price dish_quantity
0 Rs 20 off 012 Sandwich 65 2
1 Rs 20 off 013 Chicken 125 3
2 Rs 20 off 013 Chicken 125 3
3 Rs 20 off 013 Chicken 125 3
ratings reviews coupon_type user_id order_id meals order_area
4 blah blah blah Rs 20 off 9 9 5 London
4 blah blah blah Rs 20 off 9 9 5 London
3 blah blah blah Rs 20 off 9 9 5 London
4 blah blah blah Rs 20 off 9 9 5 London
我正在 dish_name 列上进行分组。
df_dish_name = df_final.groupby('dish_name')
然后我在groupby上做一些ratio操作
这给了我以下 pandas 系列..我存储在 dish_specific_perf
dish_name
Chicken 45.000000
Sandwich 61.111111
然后我在 if 循环中检查一个条件..
if((dish_specific_perf < 50).any() == True):
如果条件为真,我想将 ("NP") 字符串添加到数据框中相应的菜名中。因此,在数据框中应该如下所示。
coupon_type dish_id dish_name dish_price dish_quantity
0 Rs 20 off 012 Sandwich 65 2
1 Rs 20 off 013 Chicken 125 3
2 Rs 20 off 013 Chicken 125 3
3 Rs 20 off 013 Chicken 125 3
ratings reviews coupon_type user_id order_id meals order_area
4 blah blah blah Rs 20 off 9 9 5 London
4 blah blah blah Rs 20 off 9 9 5 London
3 blah blah blah Rs 20 off 9 9 5 London
4 blah blah blah Rs 20 off 9 9 5 London
Flag
Null
NP
NP
NP
问题是我如何将系列元素与数据框 dish_name 列进行比较以检查鸡是否存在?
当我
dish_specific_perf[0]
它只是给了我一个数字 45。
请帮忙..
本质上,您正在查找我们可以在布尔系列上使用 map
的查找,因此以下内容将添加一个布尔标志:
df_final['Flag'] = df_final['dish_name'].map(dish_specific_perf < 50)
这通过根据系列索引查找 df 值并返回该值来实现。
然后您可以将布尔值转换为您想要的标志:
df_final['Flag'] = np.where(df_final['Flag'], 'NP', 'Null')
首先,您的 if
陈述不符合您的需要。我会像这样在组中循环完成整个事情:
for name, group in df_dish_name:
# Whatever awesome thing you are doing, which give you the ratio...
if ratio < 50:
df_final.loc[df_final['dish_name']==name, 'Flag'] = 'NP'
这将避免多次索引和选择,并且更易于维护。