I have a dataframe that sums the $ amount of money that a customer has in their account during a particular month. If the customer has no money, the amount is simply 0. The dataframe looks like the below:
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
When a customer first becomes live, they go from having 0 to having a nominal amount for the particular month (or they begin with a nominal amount in Nov). Therefore when a particular customer has their first nominal amount, this is the month in which they are "new".
I want to add a column at the end of the dataframe which sums the amount for "new" customers.
I have been able to count the number of "new" customers (see code below) but I can't alter the code to sum the values.
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)
The resulting dataframe should look like this:
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