Python Pandas group by function -


i have table

    uname   sid usage 0   ahmad     5 1   ahmad     7 2   ahmad     10 3   ahmad   b   2 4   mohamad c   6 5   mohamad c   7 6   mohamad c   9 

i want group uname , side, , have usage column = group.max - group.min. if group count 1 return group max

the out put should

    uname   sid usage 0   ahmad     5 1   ahmad   b   2 2   mohamad c   3     

first, use agg grab min, max, , size of each group.
multiply min size > 1. when is, equal min, else 0. subtract max.

d1 = df.groupby(['uname', 'sid']).usage.agg(['min', 'max', 'size']) d1['max'].sub(d1['min'].mul(d1['size'].gt(1))).reset_index(name='usage') 

enter image description here


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -