{"id":2525,"date":"2023-07-21T21:45:15","date_gmt":"2023-07-21T21:45:15","guid":{"rendered":"https:\/\/statorials.org\/it\/i-panda-leggono-html\/"},"modified":"2023-07-21T21:45:15","modified_gmt":"2023-07-21T21:45:15","slug":"i-panda-leggono-html","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/i-panda-leggono-html\/","title":{"rendered":"Come leggere le tabelle html con pandas (incluso un esempio)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la funzione panda <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.read_html.html\" target=\"_blank\" rel=\"noopener\">read_html()<\/a> per leggere le tabelle HTML in un DataFrame panda.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questa funzione utilizza la seguente sintassi di base:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df = pd. <span style=\"color: #3366ff;\">read_html<\/span> (' <span style=\"color: #ff0000;\">https:\/\/en.wikipedia.org\/wiki\/National_Basketball_Association<\/span> ')\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come utilizzare questa funzione per leggere una tabella di nomi di squadre NBA da <a href=\"https:\/\/en.wikipedia.org\/wiki\/National_Basketball_Association\" target=\"_blank\" rel=\"noopener\">questa pagina Wikipedia<\/a> .<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio: leggere una tabella HTML con Panda<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Prima di utilizzare la funzione <strong>read_html()<\/strong> , probabilmente dovrai installare lxml:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong>pip <span style=\"color: #008000;\">install<\/span> lxml<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : se si utilizza un notebook Jupyter, \u00e8 necessario riavviare il kernel dopo aver eseguito questa installazione.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Successivamente, possiamo utilizzare la funzione <strong>read_html()<\/strong> per leggere ogni tabella HTML su <a href=\"https:\/\/en.wikipedia.org\/wiki\/National_Basketball_Association\" target=\"_blank\" rel=\"noopener\">questa pagina Wikipedia<\/a> :<\/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<span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n<span style=\"color: #008000;\">import<\/span> matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span> <span style=\"color: #008000;\">as<\/span> plt\n<span style=\"color: #008000;\">from<\/span> unicodedata <span style=\"color: #008000;\">import<\/span> normalize\n\n<span style=\"color: #008080;\">#read all HTML tables from specific URL\n<\/span>tabs = pd. <span style=\"color: #3366ff;\">read_html<\/span> (' <span style=\"color: #ff0000;\">https:\/\/en.wikipedia.org\/wiki\/National_Basketball_Association<\/span> ')\n\n<span style=\"color: #008080;\">#display total number of tables read\n<\/span><span style=\"color: #008000;\">len<\/span> (tabs)\n\n44<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Possiamo vedere che in questa pagina sono state trovate un totale di 44 tabelle HTML.<\/span><\/p>\n<p> <span style=\"color: #000000;\">So che la tabella che mi interessa contiene la parola &#8220;Division&#8221;, quindi posso utilizzare l&#8217;argomento <strong>match<\/strong> per recuperare solo le tabelle HTML contenenti questa parola:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#read HTML tables from specific URL with the word \"Division\" in them\n<\/span>tabs = pd. <span style=\"color: #3366ff;\">read_html<\/span> (' <span style=\"color: #ff0000;\">https:\/\/en.wikipedia.org\/wiki\/National_Basketball_Association<\/span> ',\n                    match=' <span style=\"color: #ff0000;\">Division<\/span> ')\n\n<span style=\"color: #008080;\">#display total number of tables read\n<\/span><span style=\"color: #008000;\">len<\/span> (tabs)\n\n1<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Posso quindi <a href=\"https:\/\/statorials.org\/it\/panda-elenca-i-nomi-delle-colonne\/\" target=\"_blank\" rel=\"noopener\">elencare i nomi<\/a> delle colonne della tabella:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define table\n<span style=\"color: #000000;\">df = tabs[0]<\/span>\n\n#list all column names of table\n<span style=\"color: #000000;\"><span style=\"color: #008000;\">list<\/span> (df)\n\n[('Division', 'Eastern Conference'),\n ('Team', 'Eastern Conference'),\n ('Location', 'Eastern Conference'),\n ('Arena', 'Eastern Conference'),\n ('Capacity', 'Eastern Conference'),\n ('Coordinates', 'Eastern Conference'),\n ('Founded', 'Eastern Conference'),\n ('Joined', 'Eastern Conference'),\n ('Unnamed: 8_level_0', 'Eastern Conference')]<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Sono interessato solo alle prime due colonne, quindi posso <a href=\"https:\/\/statorials.org\/it\/panda-loc-contro-iloc\/\" target=\"_blank\" rel=\"noopener\">filtrare<\/a> DataFrame per contenere solo queste colonne:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#filter DataFrame to only contain first two columns\n<\/span>df_final = df. <span style=\"color: #3366ff;\">iloc<\/span> [:, 0:2]\n\n<span style=\"color: #008080;\">#rename columns\n<\/span>df_final. <span style=\"color: #3366ff;\">columns<\/span> = [' <span style=\"color: #ff0000;\">Division<\/span> ', ' <span style=\"color: #ff0000;\">Team<\/span> ']\n\n<span style=\"color: #008080;\">#view first few rows of final DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> ( <span style=\"color: #3366ff;\">df_final.head<\/span> ())\n\n   Division Team\n0 Atlantic Boston Celtics\n1 Atlantic Brooklyn Nets\n2 Atlantic New York Knicks\n3 Atlantic Philadelphia 76ers\n4 Atlantic Toronto Raptors\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Il tavolo finale contiene solo le colonne \u201cDivisione\u201d e \u201cSquadra\u201d.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come leggere altri tipi di file in panda:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/i-panda-leggono-il-file-di-testo\/\" target=\"_blank\" rel=\"noopener\">Come leggere un file di testo con Pandas<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-leggono-excel\/\" target=\"_blank\" rel=\"noopener\">Come leggere file Excel con Panda<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/i-panda-leggono-csv\/\" target=\"_blank\" rel=\"noopener\">Come leggere file CSV con Pandas<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la funzione panda read_html() per leggere le tabelle HTML in un DataFrame panda. Questa funzione utilizza la seguente sintassi di base: df = pd. read_html (&#8216; https:\/\/en.wikipedia.org\/wiki\/National_Basketball_Association &#8216;) L&#8217;esempio seguente mostra come utilizzare questa funzione per leggere una tabella di nomi di squadre NBA da questa pagina Wikipedia . Esempio: leggere una [&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 leggere le tabelle HTML con Panda (con esempio) - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come leggere le tabelle HTML con i 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\/i-panda-leggono-html\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come leggere le tabelle HTML con Panda (con esempio) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come leggere le tabelle HTML con i panda, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/i-panda-leggono-html\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-21T21:45:15+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\/i-panda-leggono-html\/\",\"url\":\"https:\/\/statorials.org\/it\/i-panda-leggono-html\/\",\"name\":\"Come leggere le tabelle HTML con Panda (con esempio) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-21T21:45:15+00:00\",\"dateModified\":\"2023-07-21T21:45:15+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come leggere le tabelle HTML con i panda, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/i-panda-leggono-html\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/i-panda-leggono-html\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/i-panda-leggono-html\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come leggere le tabelle html con pandas (incluso un 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 leggere le tabelle HTML con Panda (con esempio) - Statorials","description":"Questo tutorial spiega come leggere le tabelle HTML con i 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\/i-panda-leggono-html\/","og_locale":"it_IT","og_type":"article","og_title":"Come leggere le tabelle HTML con Panda (con esempio) - Statorials","og_description":"Questo tutorial spiega come leggere le tabelle HTML con i panda, con un esempio.","og_url":"https:\/\/statorials.org\/it\/i-panda-leggono-html\/","og_site_name":"Statorials","article_published_time":"2023-07-21T21:45:15+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\/i-panda-leggono-html\/","url":"https:\/\/statorials.org\/it\/i-panda-leggono-html\/","name":"Come leggere le tabelle HTML con Panda (con esempio) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-21T21:45:15+00:00","dateModified":"2023-07-21T21:45:15+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come leggere le tabelle HTML con i panda, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/i-panda-leggono-html\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/i-panda-leggono-html\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/i-panda-leggono-html\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come leggere le tabelle html con pandas (incluso un 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\/2525"}],"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=2525"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2525\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}