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
Solution 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')
Solution 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.
Post a Comment for "How To Extract Pandas Series Element And Compare It With Rows In Dataframe's Column"