{"id":4088,"date":"2023-07-13T16:48:18","date_gmt":"2023-07-13T16:48:18","guid":{"rendered":"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/"},"modified":"2023-07-13T16:48:18","modified_gmt":"2023-07-13T16:48:18","slug":"i-panda-leggono-excel-dtype","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/","title":{"rendered":"Panda: come specificare i tipi durante l&#39;importazione di un file excel"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi di base per specificare il tipo di ciascuna colonna in un DataFrame quando si importa un file Excel in panda:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df = pd. <span style=\"color: #3366ff;\">read_excel<\/span> (' <span style=\"color: #ff0000;\">my_data.xlsx<\/span> ',\n                 dtype = {' <span style=\"color: #ff0000;\">col1<\/span> ': <span style=\"color: #008000;\">str<\/span> , ' <span style=\"color: #ff0000;\">col2<\/span> ': <span style=\"color: #008000;\">float<\/span> , ' <span style=\"color: #ff0000;\">col3<\/span> ': <span style=\"color: #008000;\">int<\/span> })\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">L&#8217;argomento <b>dtype<\/b> specifica il tipo di dati che ogni colonna dovrebbe avere quando si importa il file Excel in un DataFrame panda.<\/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: specificare i tipi durante l&#8217;importazione di un file Excel in Pandas<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Diciamo che abbiamo il seguente file Excel chiamato <strong>player_data.xlsx<\/strong> :<\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-32506 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/sauter-excel1.png\" alt=\"\" width=\"510\" height=\"470\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Se importiamo il file Excel utilizzando la funzione <strong>read_excel()<\/strong> , i panda tenteranno di identificare automaticamente il tipo di dati per ciascuna colonna:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <b><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#import Excel file\n<\/span>df = pd. <span style=\"color: #3366ff;\">read_excel<\/span> (' <span style=\"color: #ff0000;\">player_data.xlsx<\/span> ')\n\n<span style=\"color: #008080;\">#view resulting DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  team points rebound assists\n0 to 24 8 5\n1 B 20 12 3\n2 C 15 4 7\n3 D 19 4 8\n4 E 32 6 8\n5 F 13 7 9\n\n<span style=\"color: #008080;\">#view data type of each column\n<\/span><span style=\"color: #008000;\">print<\/span> ( <span style=\"color: #3366ff;\">df.dtypes<\/span> )\n\nteam object\nint64 dots\nrebounds int64\nassists int64\ndtype:object\n<\/strong><\/span><\/b><\/pre>\n<p> <span style=\"color: #000000;\">Dal risultato, possiamo vedere che le colonne del DataFrame hanno i seguenti tipi di dati:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>squadra<\/strong> : oggetto<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>punti<\/strong> : int64<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>rimbalza<\/strong> : int64<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>assiste<\/strong> : int64<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Tuttavia, possiamo utilizzare l&#8217;argomento <b>dtype<\/b> nella funzione <strong>read_excel()<\/strong> per specificare i tipi di dati che ciascuna colonna dovrebbe avere:<\/span><b> <\/b><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"><b><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#import Excel file and specify dtypes of columns\n<\/span>df = pd. <span style=\"color: #3366ff;\">read_excel<\/span> (' <span style=\"color: #ff0000;\">player_data.xlsx<\/span> ',\n                   dtype = {' <span style=\"color: #ff0000;\">team<\/span> ': <span style=\"color: #008000;\">str<\/span> , ' <span style=\"color: #ff0000;\">points<\/span> ': <span style=\"color: #008000;\">float<\/span> , ' <span style=\"color: #ff0000;\">rebounds<\/span> ': <span style=\"color: #008000;\">int<\/span> ,\n                            ' <span style=\"color: #ff0000;\">assists<\/span> ': <span style=\"color: #008000;\">float<\/span> })\n\n<span style=\"color: #008080;\">#view resulting DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  team points rebound assists\n0 A 24.0 8 5.0\n1 B 20.0 12 3.0\n2 C 15.0 4 7.0\n3 D 19.0 4 8.0\n4 E 32.0 6 8.0\n5 F 13.0 7 9.0\n\n<span style=\"color: #008080;\">#view data type of each column\n<\/span><span style=\"color: #008000;\">print<\/span> ( <span style=\"color: #3366ff;\">df.dtypes<\/span> )\n\nteam object\nfloat64 points\nrebounds int32\nassist float64\ndtype:object<\/strong><\/span><\/b><\/pre>\n<p><span style=\"color: #000000;\">Dal risultato, possiamo vedere che le colonne del DataFrame hanno i seguenti tipi di dati:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>squadra<\/strong> : oggetto<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>punti<\/strong> : float64<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>rimbalza<\/strong> : int32<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>assist<\/strong> : float64<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Questi tipi di dati corrispondono a quelli che abbiamo specificato utilizzando l&#8217;argomento <strong>dtype<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tieni presente che in questo esempio abbiamo specificato il tipo per ciascuna colonna nel DataFrame.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tuttavia, puoi scegliere di specificare il tipo solo per colonne specifiche e consentire ai panda di dedurre il tipo per le colonne rimanenti.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa della funzione panda <strong>read_excel()<\/strong> <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.read_excel.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\/i-panda-leggono-le-righe-saltate-di-excel\/\" target=\"_blank\" rel=\"noopener\">Panda: come saltare le righe durante la lettura di un file Excel<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/combinare-piu-fogli-excel-di-panda\/\" target=\"_blank\" rel=\"noopener\">Panda: come combinare pi\u00f9 fogli Excel<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/scrivere-diversi-fogli-excel-di-panda\/\" target=\"_blank\" rel=\"noopener\">Panda: come scrivere DataFrames su pi\u00f9 fogli Excel<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi di base per specificare il tipo di ciascuna colonna in un DataFrame quando si importa un file Excel in panda: df = pd. read_excel (&#8216; my_data.xlsx &#8216;, dtype = {&#8216; col1 &#8216;: str , &#8216; col2 &#8216;: float , &#8216; col3 &#8216;: int }) L&#8217;argomento dtype specifica il tipo [&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 specificare i tipi durante l&#039;importazione di un file Excel - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come specificare il tipo di colonna quando si importa un file Excel 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\/i-panda-leggono-excel-dtype\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Panda: come specificare i tipi durante l&#039;importazione di un file Excel - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come specificare il tipo di colonna quando si importa un file Excel in un DataFrame panda, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-13T16:48:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/sauter-excel1.png\" \/>\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-excel-dtype\/\",\"url\":\"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/\",\"name\":\"Panda: come specificare i tipi durante l&#39;importazione di un file Excel - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-13T16:48:18+00:00\",\"dateModified\":\"2023-07-13T16:48:18+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come specificare il tipo di colonna quando si importa un file Excel in un DataFrame panda, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Panda: come specificare i tipi durante l&#39;importazione di un file excel\"}]},{\"@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 specificare i tipi durante l&#39;importazione di un file Excel - Statorials","description":"Questo tutorial spiega come specificare il tipo di colonna quando si importa un file Excel 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\/i-panda-leggono-excel-dtype\/","og_locale":"it_IT","og_type":"article","og_title":"Panda: come specificare i tipi durante l&#39;importazione di un file Excel - Statorials","og_description":"Questo tutorial spiega come specificare il tipo di colonna quando si importa un file Excel in un DataFrame panda, con un esempio.","og_url":"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/","og_site_name":"Statorials","article_published_time":"2023-07-13T16:48:18+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/sauter-excel1.png"}],"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-excel-dtype\/","url":"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/","name":"Panda: come specificare i tipi durante l&#39;importazione di un file Excel - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-13T16:48:18+00:00","dateModified":"2023-07-13T16:48:18+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come specificare il tipo di colonna quando si importa un file Excel in un DataFrame panda, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/i-panda-leggono-excel-dtype\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Panda: come specificare i tipi durante l&#39;importazione di un file excel"}]},{"@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\/4088"}],"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=4088"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4088\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}