python - How to set a minimum value when performing cumsum on a dataframe column (physical inventory cannot go below 0) -


how perform cumulative sum minimum value in python/pandas?

in table below:

  • the "change in inventory" column reflects daily sales/new stock purchases.
  • data entry/human errors mean applying cumsum shows negative inventory level of -5 not physically possible.
  • as shown "inventory" column, data entry errors continue problem @ end (100 vs 95).

dataframe

            change in inventory  inventory  cumsum 2015-01-01                  100        100     100 2015-01-02                  -20         80      80 2015-01-03                  -30         50      50 2015-01-04                  -40         10      10 2015-01-05                  -15          0      -5 2015-01-06                  100        100      95 

one way achieve use loops messy , there more efficient way this.

here code generate dataframe:

import pandas pd df = pd.dataframe.from_dict({'change in inventory': {'2015-01-01': 100,   '2015-01-02': -20,   '2015-01-03': -30,   '2015-01-04': -40,   '2015-01-05': -15,   '2015-01-06': 100},  'inventory': {'2015-01-01': 100,   '2015-01-02': 80,   '2015-01-03': 50,   '2015-01-04': 10,   '2015-01-05': 0,   '2015-01-06': 100}})  df['cumsum'] = df['change in inventory'].cumsum() df 

how apply cumulative sum minimum value in python/pandas produce values shown in "inventory" column?

you can use looping, unfortunately:

lastvalue = 0 newcum = [] row in df['change in inventory']:     thisvalue =  row + lastvalue     if thisvalue < 0:         thisvalue = 0     newcum.append( thisvalue )     lastvalue = thisvalue  print pd.series(newcum, index=df.index) 2015-01-01    100 2015-01-02     80 2015-01-03     50 2015-01-04     10 2015-01-05      0 2015-01-06    100 dtype: int64 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -