Issue
filt=Q3_df['Store']=1;
Q3_df.loc[filt,'Weekly_Sales'].mean()
# >>>output=1555264.3975524479
# Q3_df dataset#
Store Weekly_Sales
1 1643690.90
1 1641957.44
1 1611968.17
1 1409727.59
1 1554806.68
... ... ... ...
45 713173.95
45 733455.07
Solution
Are you looking for a groupby
aggregation?
Q3_df.groupby(Q3_df.index)[['Weekly_Sales']].mean()
Just to articulate with a small example:
import pandas as pd
Q3_df = pd.DataFrame({"Store":[1,1,2,2,3,3], "Weekly_Sales":list(range(6))})
If Store
is not the index:
Q3_df.groupby(["Store"])[["Weekly_Sales"]].mean()
If Store
is the index:
Q3_df.set_index("Store", inplace=True)
Q3_df.groupby(Q3_df.index)[["Weekly_Sales"]].mean()
With this specific example, they both give:
Weekly_Sales
Store
1 0.5
2 2.5
3 4.5
If Store
is the index, you can assign those mean values to a new column:
Q3_df.assign(mean_values=Q3_df.groupby(Q3_df.index)[["Weekly_Sales"]].mean())
OUTPUT
Weekly_Sales mean_values
Store
1 0 0.5
1 1 0.5
2 2 2.5
2 3 2.5
3 4 4.5
3 5 4.5
Answered By - nikeros
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.