对时间序列中首次客户的金额求和

2024-06-16 17:58:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框,它计算客户在某个特定月份账户中的美元金额。如果客户没有钱,金额就是0。数据帧如下所示:

            A       B       C       D       E        F   
11/30/2015  0       1000    0       0       5000     0   
12/31/2015  2000    1000    0       3000    5000     2000
1/31/2016   2000    0       0       3000    5000     2000
2/29/2016   2000    2000    4000    3000    5000     2000
3/31/2016   2000    2000    4000    0       10000    2000
4/30/2016   0       2000    4000    0       10000    0   
5/31/2016   0       2000    4000    0       10000    0   

当客户第一次上线时,他们会从0变为某个月的名义金额(或者从11月的名义金额开始)。因此,当一个特定的客户有他们的第一个名义金额,这是一个月,他们是“新的”。你知道吗

我想在数据帧的末尾添加一列,对“新”客户的金额求和。你知道吗

我已经能够计算“新”客户的数量(见下面的代码),但是我不能改变代码来求和。你知道吗

def new_customer(column):
    return column[-1] and not any(column[:-1])
table['new_loans'] = table.iloc[:, len(table.columns)].expanding().apply(new_customer).sum(axis=1).astype(int)

生成的数据帧应如下所示:

            A       B       C       D       E        F       New_Customers 
11/30/2015  0       1000    0       0       5000     0       6000 
12/31/2015  2000    1000    0       3000    5000     2000    7000
1/31/2016   2000    0       0       3000    5000     2000    0
2/29/2016   2000    2000    4000    3000    5000     2000    4000
3/31/2016   2000    2000    4000    0       10000    2000    0
4/30/2016   0       2000    4000    0       10000    0       0
5/31/2016   0       2000    4000    0       10000    0       0

Tags: 数据代码new数量客户deftablecolumn
1条回答
网友
1楼 · 发布于 2024-06-16 17:58:43

用途:

df['New_Customers'] = df.where(df.ne(0).cumsum().eq(1)).sum(axis=1)
print (df)
               A     B     C     D      E     F  New_Customers
11/30/2015     0  1000     0     0   5000     0         6000.0
12/31/2015  2000  1000     0  3000   5000  2000         7000.0
1/31/2016   2000     0     0  3000   5000  2000            0.0
2/29/2016   2000  2000  4000  3000   5000  2000         4000.0
3/31/2016   2000  2000  4000     0  10000  2000            0.0
4/30/2016      0  2000  4000     0  10000     0            0.0
5/31/2016      0  2000  4000     0  10000     0            0.0

解释:

首先比较^{}!=)和0

print (df.ne(0))
                A      B      C      D     E      F
11/30/2015  False   True  False  False  True  False
12/31/2015   True   True  False   True  True   True
1/31/2016    True  False  False   True  True   True
2/29/2016    True   True   True   True  True   True
3/31/2016    True   True   True  False  True   True
4/30/2016   False   True   True  False  True  False
5/31/2016   False   True   True  False  True  False

^{}得到的布尔掩码的累计和:

print (df.ne(0).cumsum())
            A  B  C  D  E  F
11/30/2015  0  1  0  0  1  0
12/31/2015  1  2  0  1  2  1
1/31/2016   2  2  0  2  3  2
2/29/2016   3  3  1  3  4  3
3/31/2016   4  4  2  3  5  4
4/30/2016   4  5  3  3  6  4
5/31/2016   4  6  4  3  7  4

通过1^{}(==)-第一个1比较:

print (df.ne(0).cumsum().eq(1))
                A      B      C      D      E      F
11/30/2015  False   True  False  False   True  False
12/31/2015   True  False  False   True  False   True
1/31/2016   False  False  False  False  False  False
2/29/2016   False  False   True  False  False  False
3/31/2016   False  False  False  False  False  False
4/30/2016   False  False  False  False  False  False
5/31/2016   False  False  False  False  False  False

通过^{}将值重设为NaNs:

print (df.where(df.ne(0).cumsum().eq(1)))
                 A       B       C       D       E       F
11/30/2015     NaN  1000.0     NaN     NaN  5000.0     NaN
12/31/2015  2000.0     NaN     NaN  3000.0     NaN  2000.0
1/31/2016      NaN     NaN     NaN     NaN     NaN     NaN
2/29/2016      NaN     NaN  4000.0     NaN     NaN     NaN
3/31/2016      NaN     NaN     NaN     NaN     NaN     NaN
4/30/2016      NaN     NaN     NaN     NaN     NaN     NaN
5/31/2016      NaN     NaN     NaN     NaN     NaN     NaN

每列的最后sum

print (df.where(df.ne(0).cumsum().eq(1)).sum(axis=1))
11/30/2015    6000.0
12/31/2015    7000.0
1/31/2016        0.0
2/29/2016     4000.0
3/31/2016        0.0
4/30/2016        0.0
5/31/2016        0.0
dtype: float64

相关问题 更多 >