从其他列的值在pandas中创建新列

时间:2017-03-30 17:40:06

标签: python pandas dataframe

我正在处理以下数据框:

          p                      q 
   0      11                     2                            
   1      11                     2                  
   2      11                     2                  
   3      11                     3                  
   4      11                     3                  
   5      12                     2                  
   6      12                     2                  
   7      13                     2               
   8      13                     2            

我想创建一个新列,比如说 s ,它以0开头并继续。这个新的col基于“p”列,每当p改变时,“s”也应该改变。

对于前4行,“p”= 11,所以“s”列的前4行应该为0,依此类推......

以下是期待的df:

     s             p         q 

 0   0             11        2         
 1   0             11        2         
 2   0             11        2          
 3   0             11        2          
 4   1             11        4           
 5   1             11        4           
 6   1             11        4           
 7   1             11        4           
 8   2             12        2           
 9   2             12        2           
 10  2             12        2           
 11  3             12        3           
 12  3             12        3        

2 个答案:

答案 0 :(得分:3)

您需要diff cumsum(如果您希望ID从0开始,则减1):

df["finalID"] = (df.ProjID.diff() != 0).cumsum()
df

enter image description here

更新,如果您想同时考虑 voyg_id ProjID ,您可以使用 OR 条件就两列差异而言,无论哪一列发生变化,都会使最终的ID增加。

df['final_id'] = ((df.voyg_id.diff() != 0) | (df.proj_id.diff() != 0)).cumsum()
df

enter image description here

答案 1 :(得分:0)

df['final_id'] = df['ProjID'].diff().fillna(0).astype(bool).cumsum()
df

  voyg_id   ProjID  x_coord y_coord final_id
0   11  2   -70.642003  42.400712   0
1   11  2   -70.642002  42.402202   0
2   11  2   -70.641993  42.405850   0
3   11  2   -70.641975  42.409597   0
4   11  4   some_val    some_val    1
5   11  4   some_val    some_val    1
6   11  4   some_val    some_val    1
7   11  4   some_val    some_val    1
8   12  2   some_val    some_val    2
9   12  2   some_val    some_val    2
10  12  2   some_val    some_val    2
11  12  3   some_val    some_val    3
12  12  3   some_val    some_val    3
13  12  3   some_val    some_val    3
14  12  4   some_val    some_val    4
15  12  4   some_val    some_val    4
16  12  4   some_val    some_val    4
17  13  2   some_val    some_val    5
18  13  2   some_val    some_val    5