如何提取两个不同的pandas系列元素,并将其映射到数据框的列中

2024-03-29 04:52:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在熊猫中有一个数据帧,看起来像这样。。你知道吗

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

     order_area
       London
       London
       London
       London

我正在做一个叫groupby的栏目。你知道吗

df_dish_name = df_final.groupby('dish_name')

然后我在groupby上执行一些比率运算。你知道吗

下面是熊猫系列,我正在dish_specific_perf

dish_name
 Chicken       45.000000
 Sandwich      61.111111

我在groupby上做了其他比率运算。。你知道吗

下面是熊猫系列,我正在dish_relative_perf

 dish_name
 Chicken       5.000000
 Sandwich      21.111111

然后我检查if语句中的一个条件。。你知道吗

if ((dish_specific_perf < 50).any() & (dish_relative_perf > 20).any() == True):

如果条件为真,那么我想将(“NP”)字符串添加到dataframe中相应的dish name中。。所以,在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  

Flag
NP
NP
NP
NP

我用下面的代码来做。。你知道吗

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

但它只计算第一个条件,而不计算第二个条件。 是不是因为两个不同的序列在if循环后会有不同的元素?你知道吗


Tags: nameidtypenporderperfblahgroupby