pandas datframe用分类列上的条件替换数字列

时间:2019-01-16 21:05:06

标签: pandas dataframe sklearn-pandas

我的数据框为:

    Final_Status    Delivered_cnt   RTO_cnt
1   RTO             0               1
2   RTO             4               1
3   Delivered       4               1
4   RTO             5               1
5   RTO             2               3
6   Delivered       1               1

基于Final_Status列的值, 我想更新“已交付”和“ RTO”值。

例如: 第一行Final_Status是RTO,RTO_cnt减少1.so row1变为:

RTO             0           0

否则,如果Final_Status已交付,Delivered_cnt减少1.,则row3变为:

Delivered       3               1

最终表应该是这样的:

    Final_Status    Delivered_cnt   RTO_cnt
1   RTO             0               0
2   RTO             4               0
3   Delivered       3               1
4   RTO             5               0
5   RTO             2               2
6   Delivered       0               1

1 个答案:

答案 0 :(得分:0)

这有效:

  df1['Delivered'] = df1['Delivered'].mask(df1.Final_Status == 'Delivered',  df1['Delivered']-1)
  df1['RTO'] = df1['RTO'].mask(df1.Final_Status == 'RTO',  df1['RTO']-1)