程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pandas achieve excel pivottables

編輯:Python

1. Get help information

import pandas as pddf_1 = pd.read_excel('stu.xls')help(df_1.pivot_table)

2.Function

pivot_table(values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All', observed=False, sort=True)

3. Parameters

index : The header name of the original table.Explanation: The first column of the pivot table (the header of each row of the pivot table)columns : The header name of the original table.Explanation: The first row of the pivot table (the header of each column of the pivot table)values: The header name of the original table.Explanation: The content of the pivot table, which column of data is subject to.aggfunc : aggregate function, default is averagenp.sum() //summation;np.prod() //Multiply all elements;np.mean() //mean value;np.std() //Standard deviation;np.var() //variance;np.median() //median;np.power() //Power operation;np.sqrt() // square root;np.min() //minimum value;np.max() //Maximum value;np.argmin() //The subscript of the minimum value;np.argmax() //The subscript of the maximum value;np.inf //infinity;np.exp(10) //exponent with base e;np.log(10) //logarithmfill_value : blank data, set the fill contentmargins : whether to add row and column totalsdropna : The default is True, if all values ​​of the column are NaN, it will not be used as a calculated column, when False, it will be retainedmargins_name : the name of the summary row and column, the default is Allobserved : whether to display observationssort : whether the result is sorted

4. Cases

import pandas as pdimport numpy as npdf_1 = pd.read_excel('stu.xls')#aggfunc='count' countdf_1_r = pd.pivot_table(df_1, index='age', columns='name', values='score', aggfunc='count', margins=True,margins_name='Total', fill_value=0)df_1_r.to_excel('1.xlsx')


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved