Applying a custom groupby aggregate function to output a binary outcome in pandas python -
Applying a custom groupby aggregate function to output a binary outcome in pandas python -
i have dataset of trader transactions variable of involvement buy/sell
binary , takes on value of 1 f transaction purchase , 0 if sell. illustration looks follows:
trader buy/sell 1 0 b 1 b 1 b 0 c 1 c 0 c 0
i calculate net buy/sell
each trader such if trader had more 50% of trades buy, have buy/sell
of 1, if had less 50% purchase have buy/sell
of 0 , if 50% have na (and disregarded in future calculations).
so trader a, purchase proportion (number of buys)/(total number of trader) = 1/2 = 0.5 gives na.
for trader b 2/3 = 0.67 gives 1
for trader c 1/3 = 0.33 gives 0
the table should this:
trader buy/sell na b 1 c 0
ultimately want compute total aggregated number of buys, in case 1, , aggregated total number of trades (disregarding nas) in case 2. not interested in sec table, interested in aggregated number of buys , aggregated total number (count) of buy/sell
.
how can in pandas?
def categorize(x): m = x.mean() homecoming 1 if m > 0.5 else 0 if m < 0.5 else np.nan result = df.groupby(['trader'])['buy/sell'].agg([categorize, 'sum', 'count'])
for example,
import io import numpy np import pandas pd content = '''trader buy/sell 1 0 b 1 b 1 b 0 c 1 c 0 c 0''' df = pd.read_table(io.bytesio(content), sep='\s+') def categorize(x): m = x.mean() homecoming 1 if m > 0.5 else 0 if m < 0.5 else np.nan result = df.groupby(['trader'])['buy/sell'].agg([categorize, 'sum', 'count']) result = result.rename(columns={'categorize' : 'buy/sell'}) print(result)
yields
buy/sell sum count trader nan 1 2 b 1 2 3 c 0 1 3
python pandas group-by aggregate-functions
Comments
Post a Comment