0

Basically I am trying the take the previous row for the combination of ['dealer','State','city']. If I have multiple values in this combination I will get the Shifted value of this combination.

df['ShiftBY_D_S_C']= df.groupby(['dealer','State','city'])['dealer'].shift(1)

I am taking this ShiftBY_D_S_C column again and trying to take the count for the ['ShiftBY_D_S_C','State','city'] combination.

df['NewColumn'] = (df.groupby(['ShiftBY_D_S_C','State','city'])['ShiftBY_D_S_C'].transform("count"))+1

Below table shows what I am trying to do and it works well also. But when all the rows in ShiftBY_D_S_C column is nulls, this not working, as it have all null values. Any suggestions?

enter image description here

I am trying to see the NewColumn values like below when all the values in ShiftBY_D_S_C are NaN.

enter image description here

Ramsey
  • 345
  • 2
  • 5
  • 14
  • When asking pandas questions, best include your data in textual form for others to reproduce your problem, see [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples/20159305#20159305). – w-m Nov 28 '18 at 16:53

1 Answers1

1

You could simply handle the special case that you describe with an if/else case:

if df['ShiftBY_D_S_C'].isna().all():
    df['NewColumn'] = 1
else:
    df['NewColumn'] = df.groupby(...)
w-m
  • 10,772
  • 1
  • 42
  • 49
  • It is working. Thanks for the help. It worked when I use if else , but not with np.where(). However it solved my problem. – Ramsey Nov 28 '18 at 19:03