我为我的数据框中的每个特征创建了一个列来计算 pct_change 并且它工作正常,这是代码:
features_to_measure_growth_for = ['Base','Total_NMV_x','Total_orders','Frequency','Retention_rate','Inorganic_Activations', 'Inorganic_NMV', 'Organic_Activations',
'Organic_NMV', 'Visits', 'strike_rate', 'one_order', 'Two_orders',
'Three_orders', 'Four_orders']
for feature in features_to_measure_growth_for:
top_10_per_city[f"{feature} Growth %"] = top_10_per_city.groupby('polygon')[feature].pct_change()
print("Making PCT Change columns is done")
当我想创造复合增长时,我得到了所有特性中的所有
NaN
,这是代码:
def compound_growth(group, feature):
end_val = group[group['period'] == group['period'].max()][feature].iloc[0]
beg_val = group[group['period'] == group['period'].min()][feature].iloc[0]
n_periods = group.shape[0] - 1
if n_periods > 0:
return ((end_val / beg_val) ** (1 / n_periods) - 1) * 100
else:
return 0.0
for feature in features_to_measure_growth_for:
top_10_per_city[f"{feature} Compound Growth %"] = top_10_per_city.groupby('polygon').apply(compound_growth, feature = feature)
print('Making The Compoung Growth Rate is Done.')
我试过像这样在函数中打印每个变量的输出:
print(group['polygon'].iloc[0],feature,end_val, beg_val,n_periods)
输出:
6 Of October El 7osari 1 Base 51.0 43.0 11
6 October El 7osari 1 是多边形, 基础是特征, 51.0 是起始值, 43.0 是结束值, 11 是期数。
感谢您的帮助。