noo*_*oob 1 python group-by python-3.x pandas
df
order_date Month Name Year Days Data
2015-12-20 Dec 2014 1 3
2016-1-21 Jan 2014 2 3
2015-08-20 Aug 2015 1 1
2016-04-12 Apr 2016 4 1
and so on
Run Code Online (Sandbox Code Playgroud)
Code:(finding mean, min, and median of days column and finding number of order_dates month wise for each respective year)
df1 = (df.groupby(["Year", "Month Name"])
.agg(Min_days=("days", 'min'),
Avg_days=("days", 'mean'),
Median_days=('days','median'),
Count = ('order_date', 'count'))
.reset_index())
Run Code Online (Sandbox Code Playgroud)
df1
Year Month Name Min_days Avg_days Median_days Count
2015 Jan 9 12.56666666 10 4
2015 Feb 10 13.67678788 9 3
........................................................
2016 Jan 12 15.7889990 19 2
and so on...
Run Code Online (Sandbox Code Playgroud)
Issue at hand:
I am getting mean values Avg_Days column with more than 5 decimal places. I want to round off the values of means to 2 decimal places. How can I do that within the code?
.round(2)后面加就行了reset_index()。他将围绕所有浮动柱
df1 = (df.groupby(["Year", "Month Name"])
.agg(Min_days=("Days", 'min'),
Avg_days=("Days", 'mean'),
Median_days=('Days','median'),
Count = ('order_date', 'count'))
.reset_index().round(2))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5350 次 |
| 最近记录: |