{"id":3257,"date":"2023-07-18T10:46:40","date_gmt":"2023-07-18T10:46:40","guid":{"rendered":"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/"},"modified":"2023-07-18T10:46:40","modified_gmt":"2023-07-18T10:46:40","slug":"gruppo-di-panda-per-trimestre","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/","title":{"rendered":"Come raggruppare per trimestre in pandas dataframe (con esempio)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi di base per raggruppare le righe per trimestre in un DataFrame panda:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#convert date column to datetime<\/span>\ndf[' <span style=\"color: #ff0000;\">date<\/span> '] = pd. <span style=\"color: #3366ff;\">to_datetime<\/span> (df[' <span style=\"color: #ff0000;\">date<\/span> '])\n\n<span style=\"color: #008080;\">#calculate sum of values, grouped by quarter\n<\/span>df. <span style=\"color: #3366ff;\">groupby<\/span> (df[' <span style=\"color: #ff0000;\">date<\/span> ']. <span style=\"color: #3366ff;\">dt<\/span> . <span style=\"color: #3366ff;\">to_period<\/span> (' <span style=\"color: #ff0000;\">Q<\/span> '))[' <span style=\"color: #ff0000;\">values<\/span> ']. <span style=\"color: #3366ff;\">sum<\/span> ()\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questa particolare formula raggruppa le righe per trimestre nella colonna <strong>della data<\/strong> e calcola la somma della colonna <strong>dei valori<\/strong> nel DataFrame.<\/span><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come utilizzare questa sintassi nella pratica.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio: come raggruppare per trimestre in Pandas<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Diciamo che abbiamo il seguente DataFrame panda che mostra le vendite effettuate da un&#8217;azienda in date diverse:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#createDataFrame<\/span>\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">date<\/span> ': pd. <span style=\"color: #3366ff;\">date_range<\/span> (start='1\/1\/2022', freq='M', periods= <span style=\"color: #008000;\">12<\/span> ),\n                   ' <span style=\"color: #ff0000;\">sales<\/span> ': [6, 8, 10, 5, 4, 8, 8, 3, 5, 14, 8, 3]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n         dirty date\n0 2022-01-31 6\n1 2022-02-28 8\n2 2022-03-31 10\n3 2022-04-30 5\n4 2022-05-31 4\n5 2022-06-30 8\n6 2022-07-31 8\n7 2022-08-31 3\n8 2022-09-30 5\n9 2022-10-31 14\n10 2022-11-30 8\n11 2022-12-31 3<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Correlato:<\/strong> <a href=\"https:\/\/statorials.org\/it\/intervallo-di-date-del-panda\/\" target=\"_blank\" rel=\"noopener\">Come creare un intervallo di date in Pandas<\/a><\/span><\/p>\n<p> <span style=\"color: #000000;\">Per calcolare la somma delle vendite raggruppate per trimestre \u00e8 possibile utilizzare la seguente sintassi:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#convert date column to datetime and subtract one week<\/span>\ndf[' <span style=\"color: #ff0000;\">date<\/span> '] = pd. <span style=\"color: #3366ff;\">to_datetime<\/span> (df[' <span style=\"color: #ff0000;\">date<\/span> '])\n\n<span style=\"color: #008080;\">#calculate sum of sales, grouped by quarter\n<\/span>df. <span style=\"color: #3366ff;\">groupby<\/span> (df[' <span style=\"color: #ff0000;\">date<\/span> ']. <span style=\"color: #3366ff;\">dt<\/span> . <span style=\"color: #3366ff;\">to_period<\/span> (' <span style=\"color: #ff0000;\">Q<\/span> '))[' <span style=\"color: #ff0000;\">sales<\/span> ']. <span style=\"color: #3366ff;\">sum<\/span> ()\n\ndate\n2022Q1 24\n2022Q2 17\n2022Q3 16\n2022Q4 25\nFreq: Q-DEC, Name: sales, dtype: int64\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Ecco come interpretare il risultato:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Nel primo trimestre sono state effettuate complessivamente <b>24<\/b> vendite.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Nel corso del secondo trimestre sono state effettuate complessivamente <b>17<\/b> vendite.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Nel corso del terzo trimestre sono state effettuate complessivamente <b>16<\/b> vendite.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Nel corso del quarto trimestre sono state effettuate complessivamente <b>25<\/b> vendite.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare una sintassi simile per calcolare un&#8217;altra metrica, raggruppata per trimestre.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ad esempio potremmo invece calcolare il valore massimo delle vendite, raggruppate per trimestre:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#convert date column to datetime<\/span>\ndf[' <span style=\"color: #ff0000;\">date<\/span> '] = pd. <span style=\"color: #3366ff;\">to_datetime<\/span> (df[' <span style=\"color: #ff0000;\">date<\/span> '])\n\n<span style=\"color: #008080;\">#calculate max of sales, grouped by quarter\n<\/span>df. <span style=\"color: #3366ff;\">groupby<\/span> (df[' <span style=\"color: #ff0000;\">date<\/span> ']. <span style=\"color: #3366ff;\">dt<\/span> . <span style=\"color: #3366ff;\">to_period<\/span> (' <span style=\"color: #ff0000;\">Q<\/span> '))[' <span style=\"color: #ff0000;\">sales<\/span> ']. <span style=\"color: #3366ff;\">max<\/span> ()\n\ndate\n2022Q1 10\n2022Q2 8\n2022Q3 8\n2022Q4 14\nFreq: Q-DEC, Name: sales, dtype: int64\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Ecco come interpretare il risultato:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Le vendite massime in un singolo mese durante il primo trimestre sono state <strong>pari a 10<\/strong> .<\/span><\/li>\n<li> <span style=\"color: #000000;\">Le vendite massime in un singolo mese durante il secondo trimestre sono state <strong>8<\/strong> .<\/span><\/li>\n<li> <span style=\"color: #000000;\">Le vendite massime in un singolo mese durante il terzo trimestre sono state <strong>pari a 8<\/strong> .<\/span><\/li>\n<li> <span style=\"color: #000000;\">Il picco delle vendite in un singolo mese durante il quarto trimestre \u00e8 stato di <strong>14 unit\u00e0<\/strong> .<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa dell&#8217;operazione <strong>groupby<\/strong> in panda <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.DataFrame.groupby.html\" target=\"_blank\" rel=\"noopener\">qui<\/a> .<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre operazioni comuni nei panda:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/gruppo-di-panda-al-mese\/\" target=\"_blank\" rel=\"noopener\">Come raggruppare per mese in Pandas DataFrame<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/gruppo-di-panda-a-settimana\/\" target=\"_blank\" rel=\"noopener\">Come raggruppare per settimana in Pandas DataFrame<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/panda-groupby-conteggio-con-condizione\/\" target=\"_blank\" rel=\"noopener\">Panda: come utilizzare Groupby e contare in modo condizionale<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi di base per raggruppare le righe per trimestre in un DataFrame panda: #convert date column to datetime df[&#8216; date &#8216;] = pd. to_datetime (df[&#8216; date &#8216;]) #calculate sum of values, grouped by quarter df. groupby (df[&#8216; date &#8216;]. dt . to_period (&#8216; Q &#8216;))[&#8216; values &#8216;]. sum () Questa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come raggruppare per trimestre in Pandas DataFrame (con esempio) - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come raggruppare le righe per trimestre in un DataFrame panda, con un esempio.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come raggruppare per trimestre in Pandas DataFrame (con esempio) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come raggruppare le righe per trimestre in un DataFrame panda, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-18T10:46:40+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/\",\"url\":\"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/\",\"name\":\"Come raggruppare per trimestre in Pandas DataFrame (con esempio) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-18T10:46:40+00:00\",\"dateModified\":\"2023-07-18T10:46:40+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come raggruppare le righe per trimestre in un DataFrame panda, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come raggruppare per trimestre in pandas dataframe (con esempio)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come raggruppare per trimestre in Pandas DataFrame (con esempio) - Statorials","description":"Questo tutorial spiega come raggruppare le righe per trimestre in un DataFrame panda, con un esempio.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/","og_locale":"it_IT","og_type":"article","og_title":"Come raggruppare per trimestre in Pandas DataFrame (con esempio) - Statorials","og_description":"Questo tutorial spiega come raggruppare le righe per trimestre in un DataFrame panda, con un esempio.","og_url":"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/","og_site_name":"Statorials","article_published_time":"2023-07-18T10:46:40+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/","url":"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/","name":"Come raggruppare per trimestre in Pandas DataFrame (con esempio) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-18T10:46:40+00:00","dateModified":"2023-07-18T10:46:40+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come raggruppare le righe per trimestre in un DataFrame panda, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/gruppo-di-panda-per-trimestre\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come raggruppare per trimestre in pandas dataframe (con esempio)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3257"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=3257"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3257\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}