简体   繁体   English

以 pandas 中的其他两列为条件替换一列的值

[英]replacing the value of one column conditional on two other columns in pandas

I have a data-frame df:我有一个数据框df:

year ID  category 
1     1    0        
2     1    1        
3     1    1        
4     1    0        
1     2    0        
2     2    0        
3     2    1        
4     2    0        

I want to create a new column such that: for a particular 'year' if the 'category' is 1, the 'new-category' will be always 1 for the upcoming years:我想创建一个新列:对于特定的“年份”,如果“类别”为 1,则未来几年的“新类别”将始终为 1:

year ID  category new_category
1     1    0        0
2     1    1        1
3     1    1        1
4     1    0        1
1     2    0        0
2     2    0        0
3     2    1        1
4     2    0        1

I have tried if-else condition but I am getting the same 'category' column我已经尝试过 if-else 条件,但我得到了相同的“类别”列

for row in range(1,df.category[i-1]):
    df['new_category'] = df['category'].replace('0',df['category'].shift(1))

But I am not getting the desired column但我没有得到想要的专栏

TRY:尝试:

df['new_category'] = df.groupby('ID')['category'].cummax()

OUTPUT: OUTPUT:

   year  ID  category  new_category
0     1   1         0             0
1     2   1         1             1
2     3   1         1             1
3     4   1         0             1
4     1   2         0             0
5     2   2         0             0
6     3   2         1             1
7     4   2         0             1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM