I have a DataFrame with 2 cols
ColA| ColB D 2 D 12 D 15 A 20 A 40 A 60 C 60 C 55 C 70 C 45 L 45 L 23 L 10 L 5
RESULT/Output would be
D UP A UP C FLAT L DownWhere UP is result of adding up all the relevant Weights: each successive weight for each key, must be less than the previous weight. Example for UP you must have
Here's a simple technique, might not suit for all cases i.e :
def sum_t(x):
# Compare the value with previous value
m = x > x.shift()
# If all of them are increasing then return Up
if m.sum() == len(m)-1:
return 'UP'
# if all of them are decreasing then return Down
elif m.sum() == 0:
return 'DOWN'
# else return flat
else:
return 'FLAT'
df.groupby('ColA')['ColB'].apply(sum_t)
Output:
ColA
A UP
C FLAT
D UP
L DOWN
Name: ColB, dtype: object
Using diff
and crosstab
s=df.groupby('ColA').ColB.diff().dropna()#Dropna since the first value for all group is invalid
pd.crosstab(df.ColA.loc[s.index],s>0,normalize = 'index' )[True].map({1:'Up',0:'Down'}).fillna('Flat')
Out[100]:
ColA
A Up
C Flat
D Up
L Down
Name: True, dtype: object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With