简体   繁体   English

如何提取熊猫系列元素并将其与数据框列中的行进行比较

[英]how to extract pandas series element and compare it with rows in dataframe's column

I have a following dataframe.. 我有以下数据框。

     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  

I am doing groupby on dish_name column. 我在dish_name列上进行groupby。

df_dish_name = df_final.groupby('dish_name')

Then I am performing some ratio operations on groupby. 然后我在groupby上执行一些比率操作。

Which gives me following pandas series..which I am storing in dish_specific_perf 这给了我以下熊猫系列..我将其存储在dish_specific_perf中

dish_name
Chicken       45.000000
Sandwich      61.111111

Then I am checking one condition in if loop.. 然后我在if循环中检查一种情况。

if((dish_specific_perf < 50).any() == True):

If the condition is true then, I want to add ("NP") string to corresponding dish name in dataframe.. So, In dataframe it should look like this. 如果条件为真,那么我想在数据框中将(“ 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

The problem with this is how do I compare series elements with dataframe dish_name column to check whether chicken exist or not? 问题是如何将系列元素与dataframe dish_name列进行比较以检查是否存在鸡肉?

when I do 当我做

dish_specific_perf[0]  

It just gives me a number as 45. 它给我的数字是45。

Please help.. 请帮忙..

Essentially you are looking to do a lookup for that we can use map on the boolean series so the following will add a boolean flag: 本质上来说,您正在寻找一个可以在布尔系列上使用map的查询,因此下面将添加一个布尔标志:

df_final['Flag'] = df_final['dish_name'].map(dish_specific_perf < 50)

This works by looking up the df value against the series index and returning the value. 这是通过在系列索引中查找df值并返回该值来实现的。

You can then convert the boolean values to your desired flag: 然后,您可以将布尔值转换为所需的标志:

df_final['Flag'] = np.where(df_final['Flag'], 'NP', 'Null')

Your if statement is wrong for your needs, to begin with. 首先,您的if陈述对您的需求是错误的。 I would do the whole thing in the loop over groups like so: 我会像这样在整个循环中完成整个工作:

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'

This will avoid indexing and selecting multiple times, and is easier to maintain. 这样可以避免多次索引和选择,并且更易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM