Python+Pandas - Determining Months of Inactivity -


i have dataframe has:

columns = "chronological months" index = "customer id's" data = "dollars spent customer" 

i want create new column indicates how many consecutive months each client has been inactive (with spent $ in recent month having 0 value). i'm interested in last 6 months.

i can think of few ways inefficient (for example, string of if statements applied vectors), i'm hoping avoid them.

image of i'm envisioning below.

any appreciated. thanks! enter image description here

use bfill axis=1 (along columns) , isnull sum(axis=1) count

in [14]: df.bfill(axis=1).isnull().sum(axis=1) out[14]: cusomter 1     5 cusomter 2     6 cusomter 3     1 cusomter 4     5 cusomter 5     0 cusomter 6     3 cusomter 7     6 cusomter 8     2 cusomter 9     3 cusomter 10    0 dtype: int64  in [15]: df['months of inactivity'] = df.bfill(axis=1).isnull().sum(axis=1)  in [16]: df out[16]:                jan    feb    mar  april    mat   june  months of inactivity cusomter 1   300.0    nan    nan    nan    nan    nan                     5 cusomter 2     nan    nan    nan    nan    nan    nan                     6 cusomter 3     nan  100.0    nan    nan  100.0    nan                     1 cusomter 4   300.0    nan    nan    nan    nan    nan                     5 cusomter 5     nan    nan    nan    nan    nan  300.0                     0 cusomter 6     nan    nan  200.0    nan    nan    nan                     3 cusomter 7     nan    nan    nan    nan    nan    nan                     6 cusomter 8   100.0    nan    nan  100.0    nan    nan                     2 cusomter 9     nan    nan  400.0    nan    nan    nan                     3 cusomter 10  300.0    nan    nan  200.0  100.0  100.0                     0 

if blank cells - hyphens, use replace

in [31]: df out[31]:              jan  feb  mar april  mat june cusomter 1   300    -    -     -    -    - cusomter 2     -    -    -     -    -    - cusomter 3     -  100    -     -  100    - cusomter 4   300    -    -     -    -    - cusomter 5     -    -    -     -    -  300 cusomter 6     -    -  200     -    -    - cusomter 7     -    -    -     -    -    - cusomter 8   100    -    -   100    -    - cusomter 9     -    -  400     -    -    - cusomter 10  300    -    -   200  100  100  in [32]: df['inactivity'] = df.replace('-', np.nan).bfill(axis=1).isnull().sum(axis=1)  in [33]: df out[33]:              jan  feb  mar april  mat june  inactivity cusomter 1   300    -    -     -    -    -           5 cusomter 2     -    -    -     -    -    -           6 cusomter 3     -  100    -     -  100    -           1 cusomter 4   300    -    -     -    -    -           5 cusomter 5     -    -    -     -    -  300           0 cusomter 6     -    -  200     -    -    -           3 cusomter 7     -    -    -     -    -    -           6 cusomter 8   100    -    -   100    -    -           2 cusomter 9     -    -  400     -    -    -           3 cusomter 10  300    -    -   200  100  100           0 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -