我在下面输入了数据。我想将此数据转换为输出数据。我正在使用python语言以及numpy和pandas。请帮我解决。
输入数据:
Product year Total sale
0 Aviation Turbine Fuel 2000 63131
1 Aviation Turbine Fuel 2001 47453
2 Aviation Turbine Fuel 2002 52839
3 Aviation Turbine Fuel 2003 64041
4 Aviation Turbine Fuel 2004 66825
5 Aviation Turbine Fuel 2005 64335
6 Aviation Turbine Fuel 2006 63778
7 Aviation Turbine Fuel 2007 68938
8 Aviation Turbine Fuel 2008 68935
9 Aviation Turbine Fuel 2009 82631
10 Aviation Turbine Fuel 2010 101314
11 Aviation Turbine Fuel 2011 109808
12 Aviation Turbine Fuel 2012 115786
13 Aviation Turbine Fuel 2013 123527
14 Aviation Turbine Fuel 2014 139404
这是我要打印的输出。
输出:
Product Year Avg
Aviation Turbine Fuel 2010-2014 117967.8
Aviation Turbine Fuel 2005-2009 69723.4
Aviation Turbine Fuel 2000-2004 58857.8
答案 0 :(得分:4)
尝试使用pd.cut()
和df.groupby()
bins=[1999,2004,2009,2014]
labels=['2000-2004','2005-2009','2010-2014']
df['Year']=pd.cut(df.year,bins,labels=labels)
print(df.groupby('Year',as_index=False).agg({'Product':'first','Total sale':'mean'}))
输出:
Year Product Total sale
0 2000-2004 Aviation Turbine Fuel 58857.8
1 2005-2009 Aviation Turbine Fuel 69723.4
2 2010-2014 Aviation Turbine Fuel 117967.8