Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据条件生成新列_Python_Pandas - Fatal编程技术网

Python 根据条件生成新列

Python 根据条件生成新列,python,pandas,Python,Pandas,我有以下数据框: Hotel_id Month_Year Chef_Id Chef_is_Masterchef Transition_cnt Review_Polarity 2400614 May-2015 2297544 0 0 0.674450 2400614 June-2015 2297544 0

我有以下数据框:

Hotel_id    Month_Year      Chef_Id  Chef_is_Masterchef Transition_cnt    Review_Polarity  
2400614     May-2015        2297544     0                      0             0.674450    
2400614     June-2015       2297544     0                      0             0.894450    
2400614     July-2015       2297544     0                      0             0.888300   
2400614     August-2015     2297544     0                      0             0.894250    
2400614     September-2015  2297544     1                      1             0.975200    
2400614     October-2015    2297544     1                      0             0.700719    
2400614     November-2015   2297544     1                      0             0.955500    
2400614     December-2015   2297544     1                      0             0.675200    

3400614     April-2016      3297541     0                      0             0.774450      
3400614     May-2016        3297541     0                      0             0.874450    
3400614     June-2016       3297541     0                      0             0.994450    
3400614     July-2016       3297541     0                      0             0.888300   
3400614     August-2016     3297541     0                      0             0.994250    
3400614     September-2016  3297541     1                      1             0.675200    
3400614     October-2016    3297541     1                      0             0.800719    
3400614     November-2016   3297541     1                      0             0.755500    
3400614     December-2016   3297541     1                      0             0.975200
Chef\u是主厨
列中

  • 0
    表示-
    厨师不是主厨
  • 1
    表示主厨是主厨
当在
Chef\u is\u Masterchef
列中从
0
转换到
1
时,此转换在
transition\u cnt
列中指示为
1

因此,在转换点,我必须在3个月之前和之后从
Review\u Polarity
列中获取值,并使用该值生成新列

预期产出:

此外,我还需要另一个与上面相同的数据框,其中我需要从过渡点6个月前后的
Review\u Polarity
列中获取值

请注意,我必须为每个id执行此操作

此外,如果观察到,可以注意到新列(PVal_bfr_3mon和PVal_aftr_3mon)中的值是根据过渡点的-3个月和+2个月填充的。同样的概念可以假设为6个月,在过渡点我们需要-6个月和+5个月的值


因此,请告诉我解决方案。

我确信还有其他方法可以做到这一点,但首先,我们将列出通过更改标志提取的索引。对于这个列表,我将获得三个月前和两个月后的索引,如果是三个月后,我将修复这个位置。现在我们有了要提取的条件列表,我们可以用它提取原始数据帧。 接下来,我们用第一行和最后一行创建一个数据框,按酒店ID和厨师ID分组。下一步是在第一行和最后一行创建一个数据框,按酒店ID和厨师ID分组,在第一行和最后一行创建一个数据框,以便合并。 最后,我们将原始数据帧与三个月前的数据帧和两个月前的数据帧相结合

condition = df[df['Transition_cnt'] == 1].index.to_list()
new_con = []
for i in condition:
    print(i)
    x = i - 3
    y = i + 2
    new_con += [x, y]
update = df[df.index.isin(new_con)]
update.reset_index(inplace=True)
update

index   Hotel_id    Month_Year  Chef_Id Chef_is_Masterchef  Transition_cnt  Review_Polarity
0   1   2400614 June-2015   2297544 0   0   0.89445
1   6   2400614 November-2015   2297544 1   0   0.95550
2   10  3400614 June-2016   3297541 0   0   0.99445
3   15  3400614 November-2016   3297541 1   0   0.75550

up_df = update[['Hotel_id', 'Chef_Id', 'Review_Polarity']]
bfr_3mon = up_df.groupby(['Hotel_id','Chef_Id'])['Review_Polarity'].first().reset_index()
bfr_3mon.columns = ['Hotel_id', 'Chef_Id', 'PVal_bfr_3mon']
aftr_3mon = up_df.groupby(['Hotel_id','Chef_Id'])['Review_Polarity'].last().reset_index()
aftr_3mon.columns = ['Hotel_id', 'Chef_Id', 'PVal_aftr_3mon']
df = df.merge(bfr_3mon, on=['Hotel_id', 'Chef_Id'], how='inner')
df = df.merge(aftr_3mon, on=['Hotel_id', 'Chef_Id'], how='inner')

df
|    |   Hotel_id | Month_Year     |   Chef_Id |   Chef_is_Masterchef |   Transition_cnt |   Review_Polarity |   PVal_bfr_3mon |   PVal_aftr_3mon |
|---:|-----------:|:---------------|----------:|---------------------:|-----------------:|------------------:|----------------:|-----------------:|
|  0 |    2400614 | May-2015       |   2297544 |                    0 |                0 |          0.67445  |         0.89445 |           0.9555 |
|  1 |    2400614 | June-2015      |   2297544 |                    0 |                0 |          0.89445  |         0.89445 |           0.9555 |
|  2 |    2400614 | July-2015      |   2297544 |                    0 |                0 |          0.8883   |         0.89445 |           0.9555 |
|  3 |    2400614 | August-2015    |   2297544 |                    0 |                0 |          0.89425  |         0.89445 |           0.9555 |
|  4 |    2400614 | September-2015 |   2297544 |                    1 |                1 |          0.9752   |         0.89445 |           0.9555 |
|  5 |    2400614 | October-2015   |   2297544 |                    1 |                0 |          0.700719 |         0.89445 |           0.9555 |
|  6 |    2400614 | November-2015  |   2297544 |                    1 |                0 |          0.9555   |         0.89445 |           0.9555 |
|  7 |    2400614 | December-2015  |   2297544 |                    1 |                0 |          0.6752   |         0.89445 |           0.9555 |
|  8 |    3400614 | April-2016     |   3297541 |                    0 |                0 |          0.77445  |         0.99445 |           0.7555 |
|  9 |    3400614 | May-2016       |   3297541 |                    0 |                0 |          0.87445  |         0.99445 |           0.7555 |
| 10 |    3400614 | June-2016      |   3297541 |                    0 |                0 |          0.99445  |         0.99445 |           0.7555 |
| 11 |    3400614 | July-2016      |   3297541 |                    0 |                0 |          0.8883   |         0.99445 |           0.7555 |
| 12 |    3400614 | August-2016    |   3297541 |                    0 |                0 |          0.99425  |         0.99445 |           0.7555 |
| 13 |    3400614 | September-2016 |   3297541 |                    1 |                1 |          0.6752   |         0.99445 |           0.7555 |
| 14 |    3400614 | October-2016   |   3297541 |                    1 |                0 |          0.800719 |         0.99445 |           0.7555 |
| 15 |    3400614 | November-2016  |   3297541 |                    1 |                0 |          0.7555   |         0.99445 |           0.7555 |
| 16 |    3400614 | December-2016  |   3297541 |                    1 |                0 |          0.9752   |         0.99445 |           0.7555 |

请告诉我们您尝试了什么?我曾想过使用类似于此概念的时间窗口,但没有找到正确的方法来提取它。无论如何,下面的答案将满足要求。你能回答我最近的问题()吗?这个问题几乎与这个问题有关