如何提取 pandas 系列元素并将其与数据框列中的行进行比较 [英] how to extract pandas series element and compare it with rows in dataframe's column

查看:41
本文介绍了如何提取 pandas 系列元素并将其与数据框列中的行进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框..

     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 列进行分组.

I am doing groupby on dish_name column.

df_dish_name = df_final.groupby('dish_name')

然后我正在对 groupby 执行一些比率操作.

Then I am performing some ratio operations on groupby.

这给了我以下熊猫系列..我存储在dish_specific_perf中

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

dish_name
Chicken       45.000000
Sandwich      61.111111

然后我在 if 循环中检查一个条件..

Then I am checking one condition in if loop..

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

如果条件为真,我想在数据框中的相应菜肴名称中添加(NP")字符串..所以,在数据框中它应该是这样的.

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

这里的问题是如何将系列元素与数据框的dish_name 列进行比较以检查鸡肉是否存在?

The problem with this is how do I compare series elements with dataframe dish_name column to check whether chicken exist or not?

当我这样做时

dish_specific_perf[0]  

它只是给了我一个数字 45.

It just gives me a number as 45.

请帮忙..

推荐答案

本质上,您希望进行查找,我们可以使用 map 在布尔系列上,因此以下将添加一个布尔标志:

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)

这是通过针对系列索引查找 df 值并返回该值来实现的.

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')

这篇关于如何提取 pandas 系列元素并将其与数据框列中的行进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆