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

时光怂恿深爱的人放手 提交于 2019-12-11 12:43:07

问题


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.

df_dish_name = df_final.groupby('dish_name')

Then I am performing some ratio operations on groupby.

Which gives me following pandas series..which I am storing in dish_specific_perf

dish_name
Chicken       45.000000
Sandwich      61.111111

Then I am checking one condition in if loop..

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.

 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?

when I do

dish_specific_perf[0]  

It just gives me a number as 45.

Please help..


回答1:


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:

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.

You can then convert the boolean values to your desired flag:

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



回答2:


Your if statement is wrong for your needs, to begin with. 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.



来源:https://stackoverflow.com/questions/34220521/how-to-extract-pandas-series-element-and-compare-it-with-rows-in-dataframes-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!