{"id":4109,"date":"2023-07-13T13:24:27","date_gmt":"2023-07-13T13:24:27","guid":{"rendered":"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/"},"modified":"2023-07-13T13:24:27","modified_gmt":"2023-07-13T13:24:27","slug":"panda-ordinati-per-catena","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/","title":{"rendered":"Panda: come ordinare dataframe in base alla colonna di stringhe"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare i seguenti metodi per ordinare le righe di un DataFrame panda in base ai valori di una particolare colonna di stringhe:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: ordinamento per colonna di stringhe (quando la colonna contiene solo caratteri)<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df = df. <span style=\"color: #3366ff;\">sort_values<\/span> (' <span style=\"color: #ff0000;\">my_string_column<\/span> ')\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: ordinamento per colonna di stringhe (quando la colonna contiene caratteri <em>e<\/em> numeri)<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create 'sort' column that contains digits from 'my_string_column'\n<\/span><span style=\"color: #000000;\">df[' <span style=\"color: #ff0000;\">sort<\/span> '] = df[' <span style=\"color: #ff0000;\">my_string_column<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">extract<\/span> (' <span style=\"color: #ff0000;\">(\\d+)<\/span> ', expand= <span style=\"color: #008000;\">False<\/span> ). <span style=\"color: #3366ff;\">astype<\/span> (int)\n<\/span>\n<span style=\"color: #008080;\">#sort rows based on digits in 'sort' column\n<\/span>df = df. <span style=\"color: #3366ff;\">sort_values<\/span> (' <span style=\"color: #ff0000;\">sort<\/span> ')\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come utilizzare ciascun metodo nella pratica.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 1: ordinamento per colonna di stringhe (quando la colonna contiene solo caratteri)<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere il seguente DataFrame panda che contiene informazioni sulle vendite di vari prodotti in un negozio di alimentari:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#createDataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">product<\/span> ': ['Apples', 'Oranges', 'Bananas', 'Lettuce', 'Beans'],\n                   ' <span style=\"color: #ff0000;\">sales<\/span> ': [18, 22, 19, 14, 29]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n   product sales\n0 Apples 18\n1 Oranges 22\n2 Bananas 19\n3 Lettuce 14\n4 Beans 29<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare la seguente sintassi per ordinare le righe del DataFrame in base alle stringhe nella colonna <strong>product<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#sort rows from A to Z based on string in 'product' column\n<\/span>df = df. <span style=\"color: #3366ff;\">sort_values<\/span> (' <span style=\"color: #ff0000;\">product<\/span> ')\n\n<span style=\"color: #008080;\">#view updated DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n   product sales\n0 Apples 18\n2 Bananas 19\n4 Beans 29\n3 Lettuce 14\n1 Oranges 22<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che le righe ora sono ordinate dalla A alla Z in base alle stringhe nella colonna <strong>Prodotto<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se invece vuoi ordinare dalla Z alla A, aggiungi semplicemente l&#8217;argomento <strong>ascending=False<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#sort rows from Z to A based on string in 'product' column\n<\/span>df = df. <span style=\"color: #3366ff;\">sort_values<\/span> (' <span style=\"color: #ff0000;\">product<\/span> ', ascending= <span style=\"color: #008000;\">False<\/span> )\n\n<span style=\"color: #008080;\">#view updated DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n   product sales\n1 Oranges 22\n3 Lettuce 14\n4 Beans 29\n2 Bananas 19\n0 Apples 18\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che le righe ora sono ordinate dalla Z alla A in base alle stringhe nella colonna <strong>Prodotto<\/strong> .<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 2: ordinamento per colonna stringa (quando la colonna contiene caratteri <em>e<\/em> numeri)<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere il seguente DataFrame panda che contiene informazioni sulle vendite di vari prodotti in un negozio di alimentari:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#createDataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">product<\/span> ': ['A3', 'A5', 'A22', 'A50', 'A2', 'A7', 'A9', 'A13'],\n                   ' <span style=\"color: #ff0000;\">sales<\/span> ': [18, 22, 19, 14, 14, 11, 20, 28]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  product sales\n0 A3 18\n1 A5 22\n2 A22 19\n3 A50 14\n4 A2 14\n5 A7 11\n6 A9 20\n7 A13 28\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che le stringhe nella colonna <strong>prodotto<\/strong> contengono sia caratteri che numeri.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se proviamo a ordinare le righe del DataFrame utilizzando i valori nella colonna <strong>product<\/strong> , le stringhe non verranno ordinate nell&#8217;ordine corretto in base ai numeri:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#sort rows based on strings in 'product' column\n<\/span>df = df. <span style=\"color: #3366ff;\">sort_values<\/span> (' <span style=\"color: #ff0000;\">product<\/span> ')\n\n<span style=\"color: #008080;\">#view updated DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  product sales\n7 A13 28\n4 A2 14\n2 A22 19\n0 A3 18\n1 A5 22\n3 A50 14\n5 A7 11\n6 A9 20\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Dobbiamo invece creare una nuova colonna temporanea chiamata <strong>sort<\/strong> che contenga solo i numeri della colonna product, quindi ordinarli in base ai valori nella colonna <strong>sort<\/strong> , quindi eliminare completamente la colonna:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#create new 'sort' column that contains digits from 'product' column\n<\/span>df[' <span style=\"color: #ff0000;\">sort<\/span> '] = df[' <span style=\"color: #ff0000;\">product<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">extract<\/span> (' <span style=\"color: #ff0000;\">(\\d+)<\/span> ', expand= <span style=\"color: #008000;\">False<\/span> ). <span style=\"color: #3366ff;\">astype<\/span> (int)\n\n<span style=\"color: #008080;\">#sort rows based on digits in 'sort' column\n<\/span>df = df. <span style=\"color: #3366ff;\">sort_values<\/span> (' <span style=\"color: #ff0000;\">sort<\/span> ')\n\n<span style=\"color: #008080;\">#drop 'sort' column\n<\/span>df = df. <span style=\"color: #3366ff;\">drop<\/span> (' <span style=\"color: #ff0000;\">sort<\/span> ', axis= <span style=\"color: #008000;\">1<\/span> )\n\n<span style=\"color: #008080;\">#view updated DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  product sales\n4 A2 14\n0 A3 18\n1 A5 22\n5 A7 11\n6 A9 20\n7 A13 28\n2 A22 19\n3 A50 14<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Nota che le righe ora sono ordinate per stringhe nella colonna <strong>del prodotto<\/strong> e i numeri sono ordinati nell&#8217;ordine corretto.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa della funzione <strong>sort_values()<\/strong> di panda <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.DataFrame.sort_values.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 attivit\u00e0 comuni nei panda:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/panda-ordina-per-data\/\" target=\"_blank\" rel=\"noopener\">Panda: come ordinare per data<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-ordinano-le-colonne-per-nome\/\" target=\"_blank\" rel=\"noopener\">Panda: come ordinare le colonne per nome<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/panda-ordina-per-indice-e-colonna\/\" target=\"_blank\" rel=\"noopener\">Panda: come ordinare sia per indice che per colonna<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare i seguenti metodi per ordinare le righe di un DataFrame panda in base ai valori di una particolare colonna di stringhe: Metodo 1: ordinamento per colonna di stringhe (quando la colonna contiene solo caratteri) df = df. sort_values (&#8216; my_string_column &#8216;) Metodo 2: ordinamento per colonna di stringhe (quando la colonna contiene [&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>Panda: come ordinare un DataFrame in base a una colonna di stringhe \u2013 Stology<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come ordinare le righe di un DataFrame panda in base ai valori di una colonna stringa, 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\/panda-ordinati-per-catena\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Panda: come ordinare un DataFrame in base a una colonna di stringhe \u2013 Stology\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come ordinare le righe di un DataFrame panda in base ai valori di una colonna stringa, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-13T13:24:27+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=\"3 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/\",\"url\":\"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/\",\"name\":\"Panda: come ordinare un DataFrame in base a una colonna di stringhe \u2013 Stology\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-13T13:24:27+00:00\",\"dateModified\":\"2023-07-13T13:24:27+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come ordinare le righe di un DataFrame panda in base ai valori di una colonna stringa, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Panda: come ordinare dataframe in base alla colonna di stringhe\"}]},{\"@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":"Panda: come ordinare un DataFrame in base a una colonna di stringhe \u2013 Stology","description":"Questo tutorial spiega come ordinare le righe di un DataFrame panda in base ai valori di una colonna stringa, 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\/panda-ordinati-per-catena\/","og_locale":"it_IT","og_type":"article","og_title":"Panda: come ordinare un DataFrame in base a una colonna di stringhe \u2013 Stology","og_description":"Questo tutorial spiega come ordinare le righe di un DataFrame panda in base ai valori di una colonna stringa, con un esempio.","og_url":"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/","og_site_name":"Statorials","article_published_time":"2023-07-13T13:24:27+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"3 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/","url":"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/","name":"Panda: come ordinare un DataFrame in base a una colonna di stringhe \u2013 Stology","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-13T13:24:27+00:00","dateModified":"2023-07-13T13:24:27+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come ordinare le righe di un DataFrame panda in base ai valori di una colonna stringa, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/panda-ordinati-per-catena\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Panda: come ordinare dataframe in base alla colonna di stringhe"}]},{"@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\/4109"}],"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=4109"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4109\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}