pivot_table()


Youtube Live session on Tkinter

Pivot tables are summarized data where data is grouped for giving meaning full insights.
pivot_table(data, values=None, index=None, columns=None,
 aggfunc='mean', fill_value=None, margins=False, dropna=True,
 margins_name='All', observed=False)
data: DataFrame
values: Columns to aggregate
index: columns or grouper
columns: columns or grouper
aggfunc: defalut numpy.mean , functions
fill_value: replace missing value
margins : bool, default False, subtotal grand total
dropna: bool , default True , remove columns if all are NaN.
observed bool, default false
This is our sample DataFrame and we created by using sales.csv file or use this
import pandas as pd 
my_dict={'sale_id':[1,2,3,4,5,6,7,8,9],
         'c_id':[2,2,1,4,2,3,2,3,2],
         'p_id':[3,4,3,2,3,3,2,2,3],
	 'product':['Monitor','CPU','Monitor','RAM','Monitor','Monitor','RAM','RAM','Monitor'],
         'qty':[2,1,3,2,3,2,3,2,2],
         'store':['ABC','DEF','ABC','DEF','ABC','DEF','ABC','DEF','ABC']}
sales = pd.DataFrame(data=my_dict)
print(sales)
sale_idc_id p_idproductqtystore
1 2 3 Monitor2ABC
224CPU1DEF
313Monitor3ABC
442RAM2DEF
523Monitor3ABC
633Monitor2DEF
722RAM3ABC
832RAM2DEF
923Monitor2ABC
Using this DataFrame we will use the method pivot_table()
Here we are getting the average value of quantity ( qty ) against each product. By using aggfunc we can get sum, max, min value. Here we used one single column product as index.
pvt=pd.pivot_table(sales,values='qty',index=['product'])
qty
product
CPU1.000000
Monitor2.400000
RAM2.333333
We can use more than one column as index. Here product and store are used.
pvt=pd.pivot_table(sales,values='qty',index=['product','store'])
qty
productstore
CPUDEF1.0
MonitorABC2.5
DEF2.0
RAMABC3.0
DEF2.0

aggfunc

default value for aggfunc is mean ( np.mean) , we can specify others line sum , max , min etc or use numpy function ( import numpy before using numpy functions). Here we are using sum to get the sum of quanity ( qty ) column.
pvt=pd.pivot_table(sales,values='qty',
    index=['product','store'],aggfunc=sum)
qty
productstore
CPUDEF1
MonitorABC10
DEF2
RAMABC3
DEF4

margins

We can display total or subtotal by using margins=True, by default it is False.
pvt=pd.pivot_table(sales,values='qty',
    index=['product','store'],aggfunc=sum,margins=True)
qty
productstore
CPUDEF1
MonitorABC10
DEF2
RAMABC3
DEF4
All20

columns

pvt=pd.pivot_table(sales,index=['product'],columns=['store'],
values=['qty'])
qty
storeABCDEF
product
CPUNaN1.0
Monitor2.52.0
RAM3.02.0

Difference between pivot_table and pivot

pivot_table() : Index column pair need not be unique and it can be used with aggragrate function list sum , average etc
pivot() :If the index column pair has multiple values then it will generate value error. We can't use aggragrate functions.
Pandas pivot() Pandas DataFrame
contains() to display and delete row based on Conditions
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer