如何在数据透视表中添加趋势线?

时间:2017-07-11 23:10:48

标签: python excel python-3.x pandas

我制作了一个Python程序,它接收一个具有以下结构的Excel输入文件:

----------------------------------------------------------------
| start date  | hour | sector | KPI_1  |KPI_2 | KPI_3 | KPI_4 |
----------------------------------------------------------------
 01-01-2016      0    AUX_534    89.10    87.2    84.9    90.12
 01-01-2016      0    BVX_234    12.92    67.2    74.9    50.50
 01-01-2016      1    AUX_534    53.26    57.1    24.20   40.43
 01-01-2016      1    BVX_234    90.95    12.0    74.45   50.76
 01-01-2016      2    AUX_534    63.21    76.1    34.1    23.34
 01-01-2016      2    BVX_234    86.90    57.4    64.4    40.98

然后我的程序执行以下操作以在具有4张(4 kpis)的数据透视表中生成此Excel输出文件:

sheet_1_KPI_1
-------------------------------------------------------------------
hour      |        0   |    1  |    2   |   3   |   4   | .....  23 |
-------------------------------------------------------------------
sector    |
-------------------------------------------------------------------         
AUX_534   |      89.10 | 53.26 | 63.21  | ...   | ... |  ..... | ... |
BVX_234   |      12.92 | 90.95 | 86.90  | ...   | ... |  ..... | ... |


sheet_2_KPI_2
-------------------------------------------------------------------
hour      |        0   |    1  |    2   |   3   |   4   | .....  23 |
-------------------------------------------------------------------
sector    |
-------------------------------------------------------------------         
AUX_534   |      87.2 | 57.1 | 76.1  | ...   | ... |  ..... | ... |
BVX_234   |      67.2 | 12.0 | 57.4  | ...   | ... |  ..... | ... |

sheet_3_KPI_3 ....
sheet_4_KPI_4 ....

这是我用来生成所描述的数据透视表的代码:

import pandas as pd
from pandas import ExcelWriter

#input 
working_excel_file_2G = pd.ExcelFile("C:\\Users\\dev\\CO_N_Hourly_01_01_2016.xlsx")
#output
writer_to_Excel = ExcelWriter("C:\\Users\\dev\\temp\\Hourly_Analysis_CO.xlsx")

hourly_data = working_excel_file.parse("Sheet-2")

pivot_table_kpi_1 = pd.pivot_table(hourly_data,index=["sector"],values=["KPI_1"], columns=["hour"])
pivot_table_kpi_2 = pd.pivot_table(hourly_data,index=["sector"],values=["KPI_2"], columns=["hour"])
pivot_table_kpi_3 = pd.pivot_table(hourly_data,index=["sector"],values=["KPI_3"], columns=["hour"])
pivot_table_kpi_4 = pd.pivot_table(hourly_data,index=["sector"],values=["KPI_4"], columns=["hour"])

print("Writing the dataframe to an Excel file")
pivot_table_kpi_1.to_excel(writer_to_Excel, "KPI_1")
pivot_table_kpi_2.to_excel(writer_to_Excel, "KPI_2")
pivot_table_kpi_3.to_excel(writer_to_Excel, "KPI_3")
pivot_table_kpi_4.to_excel(writer_to_Excel, "KPI_4")
writer_to_Excel.save()

我想在第23列的末尾添加一个趋势线,以显示每行数据的行为,即如下图所示:

Trending line that I generated with Excel

我不知道如何使用pandas或其他库来绘制此趋势线,如图所示。

对我的“表格”的建议和改进很受欢迎。

0 个答案:

没有答案