Python Pivot Tables Tutorial | Create Pandas Pivot Tables | Python Tutorial | Examples, Subtotals

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ก.ค. 2024
  • Learn all about the Pandas Pivot Table function using this tutorial! If you want to follow along a written tutorial, check out my full guide here: datagy.io/python-pivot-tables/
    To download the dataset, check this link: github.com/datagy/mediumdata/...
    0:00 Introduction
    0:26 Why Use Pivot Tables?
    0:47 Pandas Pivot Tables
    0:59 Pandas Pivot Table Function
    1:33 Writing Pandas Pivot Table Function
    3:56 Pandas Pivot Table Values
    4:56 Pandas Pivot Table aggfunc Parameter
    6:54 Pandas Pivot Table Columns
    7:40 Totals and Margins
    9:00 Conclusion
    Learn Python programming the right way!
    ----------------------------------------------------------
    ✅ Check out my eBook to get started with Python for Data Science: gumroad.com/l/introtopythonfo...
    ✅ Follow me on other platforms:
    Website: datagy.io
    Twitter: / datagyio
    Mailing List: mailchi.mp/f6a58f2d73d0/datag... (and get a FREE Pandas tip and tricks book and a FREE Guide to SQL)
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 148

  • @AdventuresOfChinchin
    @AdventuresOfChinchin 3 ปีที่แล้ว +6

    I had searched Google and youtube for an entire day to gain some clarity in this concept. Finally got the right video. Thanks!

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      I’m so glad it was helpful!

    • @idlejam86
      @idlejam86 3 ปีที่แล้ว +1

      same

    • @datagy
      @datagy  3 ปีที่แล้ว

      Thank you so much!

  • @vicka_zhurina
    @vicka_zhurina 3 ปีที่แล้ว +1

    Nik, thank you for such a clear explanation of the syntax and all the features! It’s not the first of your videos that I have seen, and each time I am amazed how simply Python might be clarified. Good luck and a lot of inspiration for your new videos!

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      Thank you so much for your kind words! I’ll have more videos coming soon! :)

  • @AnalyticInsight
    @AnalyticInsight 2 ปีที่แล้ว +1

    Thanks for your clear and complete treatment of pivot table. Easy to comprehend, started to use immediately.

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks so much for your comment! I appreciate the feedback :)

  • @gauthamambethkar4483
    @gauthamambethkar4483 3 ปีที่แล้ว +3

    The way you explained was perfect. Keep up the good work. Hoping to see a lot more data science and Machine Learning videos from you

    • @datagy
      @datagy  3 ปีที่แล้ว

      Thanks so much, Gautham! I have lots more planned :)

  • @jamesgilmore5538
    @jamesgilmore5538 2 ปีที่แล้ว

    This was the most helpful video I've seen on anything pandas. Thank you. Appreciate the simple and short examples/steps that explain the arguments.

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks so much, James! That means a lot to me!

  • @OberynPinkViper
    @OberynPinkViper 2 ปีที่แล้ว +2

    Just wanted to say thank you for the incredibly simple yet comprehensive video. I stumbled upon the pivot() method while working through data visualisation, and I wasn't 100% sure how it worked.
    After this watching this video, everything is much more clearer and understandable to me.

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you so much for your kind words! That means a lot!

  • @DrSpacemann
    @DrSpacemann 4 ปีที่แล้ว +1

    Killed it again! Very clear and easy to follow,thank you!

    • @datagy
      @datagy  4 ปีที่แล้ว

      Thanks so much! 😊

  • @rferstahl
    @rferstahl 2 ปีที่แล้ว +1

    simple and to the point, exactly what I was looking for. Thanks

    • @datagy
      @datagy  2 ปีที่แล้ว

      I’m so glad!

  • @SetoAjiNugroho
    @SetoAjiNugroho 3 ปีที่แล้ว +1

    I skipped every basics of machine learning and now i realized that i need basic like this. Thanks to made it easy to learn. Very great explained :D

    • @datagy
      @datagy  3 ปีที่แล้ว

      I’m so glad it was helpful! :)

  • @toddbrannon6262
    @toddbrannon6262 2 ปีที่แล้ว +2

    Thanks for this video. So straight to the point. I learned something I can now put to use!

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks so much, Todd!

  • @rajeeb101
    @rajeeb101 2 ปีที่แล้ว +1

    Fantastic work! Simple and to the point. Thank you.

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you so much!

  • @coachjeffnac
    @coachjeffnac 3 ปีที่แล้ว

    I normally get my info on youtube videos and go. This video quickly/easily helped me solve something I'd been struggling with! I had a groupby that I couldn't figure out how to transpose. PIVOT is what I wanted! Thanks!

    • @datagy
      @datagy  3 ปีที่แล้ว

      I’m glad it helped! :)

  • @mrescience2554
    @mrescience2554 2 ปีที่แล้ว +1

    Oh my goodness, this is exactly what I needed! Great explanation, thank you so much.

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you so much! I’m glad it was helpful!

  • @Danielmelian3
    @Danielmelian3 2 ปีที่แล้ว

    I was looking for this everywhere, Thankyou so much!

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks so much, Daniel!

  • @MayankSinghParmar
    @MayankSinghParmar ปีที่แล้ว +1

    Simple and Clear. Very good video

    • @datagy
      @datagy  ปีที่แล้ว

      Thank you so much!

  • @irvingescobar3977
    @irvingescobar3977 3 ปีที่แล้ว +1

    This is exactly the video I needed! Been looking to transition out of excel pivots due to huge data sets and this was super helpful.

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      I’m so glad it was helpful, Irving!

    • @irvingescobar3977
      @irvingescobar3977 3 ปีที่แล้ว +1

      @@datagy Hoping you do some more deep dives into data analysis soon :D

    • @datagy
      @datagy  3 ปีที่แล้ว

      Thanks!! I’ve got a lot of exciting stuff coming!

  • @rohitbhosale3617
    @rohitbhosale3617 ปีที่แล้ว

    Thank you so much!To the point.
    Love from India

  • @Dimejesi
    @Dimejesi ปีที่แล้ว

    Thank you so much. I just started learning python for data Science and you really broke this down and made it easy for me to understand

    • @datagy
      @datagy  ปีที่แล้ว

      Thank you!! That means a lot to me!

  • @bevg1
    @bevg1 2 ปีที่แล้ว

    wow! you are a good teacher... straight to the point and it is not boring. 👍

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you so much!

  • @TotallyAqua
    @TotallyAqua 3 ปีที่แล้ว +1

    Really helpful video. I liked how you explained adding totals to the rows and columns.

    • @datagy
      @datagy  3 ปีที่แล้ว

      Thanks so much!

  • @JDUDEadventure
    @JDUDEadventure ปีที่แล้ว

    Thank you video was super helpful to better understand pivot tables with pandas

  • @cocoapebbles10
    @cocoapebbles10 3 ปีที่แล้ว +2

    This is awesome dude! You really saved my bacon here. It's so difficult to learn this stuff over Zoom class.

    • @datagy
      @datagy  3 ปีที่แล้ว

      I'm so glad it was helpful!

  • @jongcheulkim7284
    @jongcheulkim7284 2 ปีที่แล้ว

    Thank you very much. This is very helpful.

  • @mapa5000
    @mapa5000 2 ปีที่แล้ว +1

    Very well explained !!! . thank you !

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks so much, Marco!

  • @luqmanhakim7781
    @luqmanhakim7781 3 ปีที่แล้ว +1

    Crystal clear explanation ! Thanks for sharing.. looking forward for more content like this :)

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      Thank you so much Luqman! I’ll be releasing more videos soon! :)

    • @luqmanhakim7781
      @luqmanhakim7781 3 ปีที่แล้ว +1

      @@datagy keep it up the good work man 👍

  • @ravirajshelar250
    @ravirajshelar250 3 ปีที่แล้ว +2

    Thanks you just made me learn it so easily.😊

    • @datagy
      @datagy  3 ปีที่แล้ว

      Thanks so much! I'm glad it was helpful!

  • @alfonsoborges4885
    @alfonsoborges4885 2 ปีที่แล้ว +1

    Thanks, great explanation !

    • @datagy
      @datagy  2 ปีที่แล้ว +1

      Thanks for your comment!

  • @azrflourish9032
    @azrflourish9032 3 ปีที่แล้ว +1

    clear explanation! thank you

    • @datagy
      @datagy  3 ปีที่แล้ว

      Thank you!

  • @SayanChakraborty10397
    @SayanChakraborty10397 3 ปีที่แล้ว +1

    This was great! Thanks a lot :)

    • @datagy
      @datagy  3 ปีที่แล้ว

      Thank you, Sayan!

  • @acesovernines
    @acesovernines 7 หลายเดือนก่อน +1

    Succinct and super effective. Thank you so much

    • @datagy
      @datagy  7 หลายเดือนก่อน

      Thanks so much!

  • @TrungNguyen-te1wm
    @TrungNguyen-te1wm 3 ปีที่แล้ว +1

    Great tutorial. Thanks

    • @datagy
      @datagy  3 ปีที่แล้ว

      Thanks Trung!

  • @d00lph1n
    @d00lph1n 2 ปีที่แล้ว +1

    Thank you. Great explanation :)

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks for your comment!

  • @SoulSheIS
    @SoulSheIS 3 ปีที่แล้ว +1

    Thank you for keeping your videos short, yet informative and engaging.

    • @datagy
      @datagy  3 ปีที่แล้ว

      I’m glad you liked it!

    • @SoulSheIS
      @SoulSheIS 3 ปีที่แล้ว +1

      @@datagy Absolutely!
      May I ask how would you know when to use the pivot method as oppose to the pivot_table method?

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      @@SoulSheIS the pivot_table method is a bit more flexible. Essentially, if you have duplicates in your data, the pivot_table method is necessary. Similarly, it allows you to use multiple indices.
      Historically, the pivot_table method is built on pivot, but was introduced to not break previous functionality.
      Hope this helps!

    • @SoulSheIS
      @SoulSheIS 3 ปีที่แล้ว +1

      @@datagy Ah, okay, makes a lot of sense. Thank you for clarifying!

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      @@SoulSheIS Glad it helped!

  • @mariaromero3488
    @mariaromero3488 ปีที่แล้ว +1

    so helpful, thanks!

    • @datagy
      @datagy  ปีที่แล้ว

      Thanks so much!

  • @nicozing4552
    @nicozing4552 2 ปีที่แล้ว +1

    Thank you so much! very clear :)

    • @datagy
      @datagy  2 ปีที่แล้ว +1

      Thanks Nico!

  • @SunGlassesBlue
    @SunGlassesBlue ปีที่แล้ว +1

    Thank you dude, you saved my life !
    Subscribing is not an option =)

    • @datagy
      @datagy  ปีที่แล้ว

      Glad it helped!

  • @krishnaRaog21
    @krishnaRaog21 3 ปีที่แล้ว +1

    Very nice ...You made things so simple

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      Thanks! I’m glad you liked it!

  • @syedrizvi2687
    @syedrizvi2687 2 ปีที่แล้ว +1

    Thank you so much!

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks so much for your comment!

  • @timfairfield407
    @timfairfield407 ปีที่แล้ว +1

    great explanation. thanks

    • @datagy
      @datagy  ปีที่แล้ว

      Thanks Tim!

  • @gvvlogsriderunrefresh6155
    @gvvlogsriderunrefresh6155 3 ปีที่แล้ว

    One of the best videos i have ever seen. So simplified and So effective

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      Thank you, Gaurav!

    • @gvvlogsriderunrefresh6155
      @gvvlogsriderunrefresh6155 3 ปีที่แล้ว +1

      @@datagy Hey Nik , Can you also put in some videos on Seaborn usage for Visualization and on Statistics in Python. It will be a great favour brother

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      Hey Gaurav! Seaborn is definitely planned (as well as plotly). What kind of statistics in python are you looking for? :) thanks for the ideas!

    • @gvvlogsriderunrefresh6155
      @gvvlogsriderunrefresh6155 3 ปีที่แล้ว +1

      Dear Nik, In statistics if you can cover working with scipy.stats , cover some tests in python etc . Thanks a lot again for your prompt response

    • @datagy
      @datagy  3 ปีที่แล้ว

      @@gvvlogsriderunrefresh6155 Thanks! I'll add this to the list!

  • @elfridhasman4181
    @elfridhasman4181 2 ปีที่แล้ว +1

    Wow, thank you very much 🙏🙏😍

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks for your comment, Elfrid!

  • @tuandino6990
    @tuandino6990 2 ปีที่แล้ว +1

    well explained!

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you!!

  • @Diamond_Hanz
    @Diamond_Hanz 3 ปีที่แล้ว +2

    NYC here...Data Science Major.

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      Welcome to the channel!

  • @boppanibhanuprasad4612
    @boppanibhanuprasad4612 2 ปีที่แล้ว +1

    Plain and simple.

    • @datagy
      @datagy  2 ปีที่แล้ว +1

      Thank you!

  • @MagnusAnand
    @MagnusAnand 2 ปีที่แล้ว +1

    Cool video

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks Guz!

  • @user-fh3gr7sq1z
    @user-fh3gr7sq1z ปีที่แล้ว

    tnx alot. how can we claculate the precentage by the total (for each column....) ?

  • @ShawVishal
    @ShawVishal 3 ปีที่แล้ว +1

    Thanks

    • @datagy
      @datagy  3 ปีที่แล้ว

      You’re welcome!

  • @ahmedhelal920
    @ahmedhelal920 2 ปีที่แล้ว

    thanks for this useful information , i am trying to add pivot on function and add this function using xlwings on excel , the function works good , but i need it come with porders GUI instead off normal cells

    • @datagy
      @datagy  2 ปีที่แล้ว

      Hi Ahmed, thanks for your comment! Could you explain what you mean by porders?

  • @86Maryj
    @86Maryj ปีที่แล้ว

    Hi can you pls to pivot with multiple sheets

  • @ravindrareddy5478
    @ravindrareddy5478 ปีที่แล้ว

    is there any way to merge empty cells using python in a pivot table

  • @yellowsun7241
    @yellowsun7241 3 ปีที่แล้ว +1

    Thanks for this. How can I make jupyter print these nice looking tables like in the video ? Mine are very plain and all over the place. Cheers!

    • @datagy
      @datagy  3 ปีที่แล้ว

      Thanks for your comment! It should work if you simply write the object name without calling print(). Hope this helps! :)

    • @yellowsun7241
      @yellowsun7241 3 ปีที่แล้ว +1

      @@datagy thanks Nick! ;)

  • @MrFishingo
    @MrFishingo 2 ปีที่แล้ว +1

    hello datagy!! two questions, how can I calculate a weighted average on the pivot table?? Second, after results are printed, how can I sort values within a column? thanks for the great videos!!

    • @datagy
      @datagy  2 ปีที่แล้ว

      Hi Ricardo! Thanks so much for your comment!
      To sort your data, you'd technically want to sort it before printing it (or in the print function). You can sort data. Such as print(df.sort_values(by=['colname'])). I have a video on this topic here: th-cam.com/video/41XLTQI1qzI/w-d-xo.html
      To create a weighted average you can follow along with my written tutorial here: datagy.io/pandas-weighted-average/
      It'll walk you through a number of different ways in which you can calculate a weighted average (including using Pandas).

  • @DIGITAL_COOKING
    @DIGITAL_COOKING 2 ปีที่แล้ว +1

    please i have one question if you want the mean values to be integers how you can do that, thanks!

    • @datagy
      @datagy  2 ปีที่แล้ว +1

      You can convert the column to integers by apply the .astype('int') method on the column of the resulting dataframe :). Such as:
      pivot['col'] = pivot['col'].astype('int')

    • @DIGITAL_COOKING
      @DIGITAL_COOKING 2 ปีที่แล้ว +1

      @@datagy thanks a lot for your help

  • @parthraval6052
    @parthraval6052 3 ปีที่แล้ว +1

    hey nice video..one problem - suppose i have values in a column starting with 5555 eg(55551,55553,55557 etc)....how to filter it out

    • @datagy
      @datagy  3 ปีที่แล้ว

      Hi Parth,
      Thanks for your comment! I'd say, and there's probably much better ways to do this, but one way you could do this is to filter a string version of the column:
      df = df[df['colname'].astype('str').str.contains('5555')]

    • @parthraval6052
      @parthraval6052 3 ปีที่แล้ว

      @@datagy thanks for your reply I will try it out 👍

  • @chinmaychourdia6929
    @chinmaychourdia6929 ปีที่แล้ว

    3:56 , What if we want only the region which has maximum sales

  • @XuanTran-ri1hn
    @XuanTran-ri1hn 2 ปีที่แล้ว +1

    Thank you very much for your video. May I ask if you can explain the value in the pivot 5? Is it average value or sum? I just need a confirmation :)

    • @datagy
      @datagy  2 ปีที่แล้ว +1

      Hi Xuan, thanks for your comment! By default, a Pandas pivot table will calculate the average value :).

    • @XuanTran-ri1hn
      @XuanTran-ri1hn 2 ปีที่แล้ว

      @@datagy Thanks :)

  • @amrimanel3855
    @amrimanel3855 3 ปีที่แล้ว +1

    thank you very much for your explanation ; handsome man. do you have a document that contains all the equation please ? the links : all of them error

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      Thanks Amri! I'm glad it was helpful!
      I'm not able to find any formal documentation on this, but you can pass in many different kinds of functions, including numpy's functions.

  • @AnjanBasumatary
    @AnjanBasumatary ปีที่แล้ว +1

    How to display emty rows and column combination? ....in the output I only get those rows and column which have values

    • @datagy
      @datagy  ปีที่แล้ว

      You can use dropna =False in the pivot_table function :)

    • @AnjanBasumatary
      @AnjanBasumatary ปีที่แล้ว

      @@datagy Table3TEST3= PL.pivot_table(values =( 'ID','IN'),index = 'SEGMENT', columns='CUTOFF',dropna= False, aggfunc = {'ID':'count','IN':sum})
      I have used dropna=False but still not working files uploaded in this link:
      drive.google.com/drive/folders/1Pnc4YMjqL0loclfX-5Nxi3KTpxFd1ffm?usp=sharing

  • @debjanidas6779
    @debjanidas6779 2 ปีที่แล้ว

    Can you just tell me what and why you made changes between 7:39 sec to 7:42 sec.
    Than you great video

  • @alcapone6796
    @alcapone6796 3 ปีที่แล้ว +1

    How do I use the pivot table values to create a histogram?

    • @datagy
      @datagy  3 ปีที่แล้ว

      You may not need to pivot anything to create the histogram, as the histogram will create bins for you automatically. I’d suggest checking out my video on histograms: th-cam.com/video/m0y5uqU--YQ/w-d-xo.html

  • @syhusada1130
    @syhusada1130 2 ปีที่แล้ว +1

    how would you pivot multiple columns?

    • @datagy
      @datagy  2 ปีที่แล้ว

      If you’re looking to aggregate multiple columns, you could place them as a list into the values parameter :)

  • @gouthamkarakavalasa4267
    @gouthamkarakavalasa4267 3 ปีที่แล้ว

    Hi can you pls let me know how to create pivot tables with mode as function, no mean. I looking a way to analyse data by pivoting on object data type with mode function. Kindly do the needful.

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      Hi there! Thanks so much for your great question!
      Import mode from scipy.stats using:
      from scipy.stats import mode
      Then, in your pivot table, as the aggfunc= use the following:
      aggfunc=lambda x: x.mode().iat[0]
      Just tested it and it works well :)

    • @gouthamkarakavalasa4267
      @gouthamkarakavalasa4267 3 ปีที่แล้ว +1

      @@datagy Hey, Thank u very much.. it worked:)

    • @datagy
      @datagy  3 ปีที่แล้ว

      I’m so glad! Thanks for letting me know :)

  • @deep.jain.
    @deep.jain. 10 หลายเดือนก่อน +1

    How to add filter to these pivot tables?

    • @datagy
      @datagy  10 หลายเดือนก่อน

      You’d filter the dataframe either beforehand or when you pass it into the data argument :)

  • @tobecontinued...5712
    @tobecontinued...5712 3 ปีที่แล้ว +1

    What if the value i want its sum is a string?? what can i do to get the sum of a string value?

    • @datagy
      @datagy  3 ปีที่แล้ว +1

      Hi there! Is the "string" a number? i.e., "9" formatted as a string?

    • @tobecontinued...5712
      @tobecontinued...5712 3 ปีที่แล้ว

      @@datagy no ,the value is " lancé, terminé, provisoire ", and i have different section, and I want the sum of the values of each section.

    • @datagy
      @datagy  3 ปีที่แล้ว

      @@tobecontinued...5712 Ah, you want the values broken out by those categories?
      So each section lance, termine, provisoire (forgive my lack of accents) would have numeric values beside it?

  • @milkway731
    @milkway731 2 ปีที่แล้ว

    i just found if I set margin, the code will fail. Traceback (most recent call last)

  • @kinanradaideh5479
    @kinanradaideh5479 ปีที่แล้ว

    @6:21

  • @seekthehorizon6854
    @seekthehorizon6854 2 ปีที่แล้ว

    😅😊

  • @hhbbhvvbjhbbyjj
    @hhbbhvvbjhbbyjj 2 ปีที่แล้ว +1

    u couldnt of made the video more clear

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks so much!!

  • @Xiler6969
    @Xiler6969 3 ปีที่แล้ว +1

    delete intro music