pivot()

Pivot are summarized data without any aggregate functions used.
pivot(index=None, columns=None, values=None)
index: str or object or a list of str, optional
columns: list or Column to use to make new frame’s columns.
values Column(s) to use for populating new frame’s values (optional ).
This is our sample DataFrame ( created by using sales.csv file after removing duplicate column/ rows )
import pandas as pd 
my_dict={'sale_id':[1,2,4,6,7],
         'c_id':[2,2,4,3,2],
         'p_id':[3,4,2,3,2],
	 'product':['Monitor','CPU','RAM','Monitor','RAM'],
         'qty':[2,1,2,2,3],
         'store':['ABC','DEF','DEF','DEF','ABC']}
sales = pd.DataFrame(data=my_dict)
print(sales)
sale_idc_idp_idproductqtystore
0123Monitor2ABC
1224CPU1DEF
2442RAM2DEF
3633Monitor2DEF
4722RAM3ABC
Using this DataFrame we will use the method pivot()
pvt=pd.pivot(sales,index=['product'],columns=['store'])
sale_idc_idp_idqty
storeABCDEFABCDEFABCDEFABCDEF
product
CPUNaN2.0NaN2.0NaN4.0NaN1.0
Monitor1.06.02.03.03.03.02.02.0
RAM7.04.02.04.02.02.03.02.0

values

pvt=sales.pivot(index=['product'],columns=['store'],values='qty')
storeABCDEF
product
CPUNaN1.0
Monitor2.02.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_table() 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