Pandas Dataframe ,以减去具有先前执行值的值

zbdgwd5y  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(150)

我想从的总计数中减去第一行的值
测试用例和具有执行计数结果的剩余值。

**Input:**
       Date Count
       17-10-2022   20
       18-10-2022   18
       19-10-2022   15
       20-10-2022   10
       21-10-2022   5
   
       **Code:**
       df['Date'] = pd.to_datetime(df['Date'])
       edate = df['Date'].max().strftime('%Y-%m-%d')
       sdate = df['Date'].min().strftime('%Y-%m-%d')
       df['Date'] = pd.to_datetime(df['Date']).apply(lambda x: x.date())
       df=df.groupby(['Date'])['Date'].count().reset_index(name='count')
       df['result'] = Test_case - df['count'].iloc[0]
       df['result'] = df['result'] - df['count'].shift(1)

       **Output generating:**
       Date  count  result
       0  2022-10-17     20     NaN
       1  2022-10-18     18    40.0
   
       **Expected Output:**
       Date Count   Result  
       17-10-2022   20  60(80-20) - 80 is the total Test case count for example
       18-10-2022   18  42(60-18)
       19-10-2022   15  27(42-15)
       20-10-2022   10  17(27-10)
       21-10-2022   5   12(17-5)
dddzy1tm

dddzy1tm1#

80是一个任意数吗?然后使用以下代码:

n = 80
df.assign(Result=df['Count'].cumsum().mul(-1).add(n))

输出:

Date      Count Result
0   17-10-2022  20  60
1   18-10-2022  18  42
2   19-10-2022  15  27
3   20-10-2022  10  17
4   21-10-2022  5   12

你可以改变n

相关问题