簡體   English   中英

根據另一列中的一列查找行值並進行計算

[英]Find row value based on one column in another column and do calculation

我有一個數據框:

import pandas as pd
data = pd.DataFrame({'start':['2020-08-01','2020-08-02','2020-08-03','2020-08-04','2020-08-05','2020-08-06','2020-08-07','2020-08-08'],
                     'end':['2020-08-03','2020-08-03','2020-08-06','2020-08-06','2020-08-06','2020-08-08','2020-08-08','2020-08-08'],
                     'score':[74, 81, 38, 49, 79, 17, 53, 69]})

我需要計算start日期與其對應的end日期之間的score差異為:

         start         end  score  result
0   2020-08-01  2020-08-03     74      36  # 74-38 as score on 08/03 is 38
1   2020-08-02  2020-08-03     81      43  # 81-38
2   2020-08-03  2020-08-06     38      21  # 38-17 as score on 08/06 is 17
3   2020-08-04  2020-08-06     49      32  # 49-17
4   2020-08-05  2020-08-06     79      62  # 79-17
5   2020-08-06  2020-08-08     17     -52  # 17-69 as score on 08/08 is 69
6   2020-08-07  2020-08-08     53     -16  # 53-69
7   2020-08-08  2020-08-08     69       0  # 69-69

有沒有好的pandas方法來做到這一點? 非常感謝!

如果使用所有的start值是映射值減去獨特:

data['result'] = data['score'].sub(data['end'].map(data.set_index('start')['score']))
print (data)
        start         end  score  result
0  2020-08-01  2020-08-03     74      36
1  2020-08-02  2020-08-03     81      43
2  2020-08-03  2020-08-06     38      21
3  2020-08-04  2020-08-06     49      32
4  2020-08-05  2020-08-06     79      62
5  2020-08-06  2020-08-08     17     -52
6  2020-08-07  2020-08-08     53     -16
7  2020-08-08  2020-08-08     69       0

詳情

print (data['end'].map(data.set_index('start')['score']))
0    38
1    38
2    17
3    17
4    17
5    69
6    69
7    69
Name: end, dtype: int64

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM