It is possible to dynamically add names to the global namespace by modifying globals()
, but it is strongly discouraged. Use a dictionary instead (as described by Shijo).
Another approach is to aggregate all columns you need on the same GroupBy
object. For example, given the following data frame
np.random.seed(0)
# generate fake data
date_range = pd.Series(pd.date_range('2017-01-01', periods=3))
df = pd.DataFrame({
'date': pd.concat([date_range] * 2),
'sales': np.random.normal(0, 1, 6),
'orders': np.random.normal(0, 1, 6)
}).reset_index(drop=True)
print(df)
date orders sales
0 2017-01-01 0.950088 1.764052
1 2017-01-02 -0.151357 0.400157
2 2017-01-03 -0.103219 0.978738
3 2017-01-01 0.410599 2.240893
4 2017-01-02 0.144044 1.867558
5 2017-01-03 1.454274 -0.977278
you can do
# the fields for which you want to compute trends
field_names = ['sales', 'orders']
# compute trends using a single GroupBy
trend = df.groupby('date', as_index=False)[field_names].mean().sort_values('date')
print(trend)
date sales orders
0 2017-01-01 2.002473 0.680343
1 2017-01-02 1.133858 -0.003657
2 2017-01-03 0.000730 0.675527
Now you can use the resulting trend
data frame similarly to a namespace. Where you had wanted to use the name sales_trend
, you can instead use trend['sales']
.
print(trend['sales'])
0 2.002473
1 1.133858
2 0.000730
Name: sales, dtype: float64