{"id":2872,"date":"2023-07-20T07:55:51","date_gmt":"2023-07-20T07:55:51","guid":{"rendered":"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/"},"modified":"2023-07-20T07:55:51","modified_gmt":"2023-07-20T07:55:51","slug":"stai-tentando-di-unire-le-colonne-object-e-int64","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/","title":{"rendered":"Come risolvere il problema: stai tentando di unire le colonne object e int64"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Un errore che potresti riscontrare quando usi i panda \u00e8:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #ff0000;\">ValueError<\/span> : You are trying to merge on int64 and object columns.\n            If you wish to proceed you should use pd.concat\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo errore si verifica quando si tenta di unire due DataFrame panda ma la colonna su cui si sta unendo \u00e8 un oggetto in un DataFrame e un numero intero nell&#8217;altro DataFrame.<\/span><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come correggere questo errore nella pratica.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Come riprodurre l&#8217;errore<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Diciamo di creare i seguenti due DataFrames panda:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import<\/span> pandas <span style=\"color: #107d3f;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#createDataFrame<\/span>\ndf1 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">year<\/span> ': [2015, 2016, 2017, 2018, 2019, 2020, 2021],\n                    ' <span style=\"color: #ff0000;\">sales<\/span> ': [500, 534, 564, 671, 700, 840, 810]})\n\ndf2 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">year<\/span> ': ['2015', '2016', '2017', '2018', '2019', '2020', '2021'],\n                    ' <span style=\"color: #ff0000;\">refunds<\/span> ': [31, 36, 40, 40, 43, 70, 62]})\n\n<span style=\"color: #008080;\">#view DataFrames\n<\/span><span style=\"color: #008000;\">print<\/span> (df1)\n\n   year sales\n0 2015 500\n1 2016 534\n2 2017 564\n3 2018 671\n4 2019 700\n5,2020 840\n6 2021 810\n\n<span style=\"color: #008000;\">print<\/span> (df2)\n\n   year refunds\n0 2015 31\n1 2016 36\n2 2017 40\n3 2018 40\n4 2019 43\n5 2020 70\n6 2021 62<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Supponiamo ora di provare a unire i due DataFrame:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#attempt to merge two DataFrames\n<\/span>big_df = df1. <span style=\"color: #3366ff;\">merge<\/span> (df2, on=' <span style=\"color: #ff0000;\">year<\/span> ', how=' <span style=\"color: #ff0000;\">left<\/span> ')\n\n<span style=\"color: #ff0000;\">ValueError<\/span> : You are trying to merge on int64 and object columns.\n            If you wish to proceed you should use pd.concat\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Riceviamo un <strong>ValueError<\/strong> perch\u00e9 la variabile <strong>year<\/strong> nel primo DataFrame \u00e8 un numero intero ma la variabile <strong>year<\/strong> nel secondo DataFrame \u00e8 un oggetto.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Come correggere l&#8217;errore<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il modo pi\u00f9 semplice per correggere questo errore \u00e8 semplicemente convertire la variabile <strong>anno<\/strong> del secondo DataFrame in un numero intero e quindi eseguire l&#8217;unione.<\/span><\/p>\n<p> <span style=\"color: #000000;\">La seguente sintassi mostra come eseguire questa operazione:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#convert year variable in df2 to integer\n<\/span>df2[' <span style=\"color: #ff0000;\">year<\/span> ']=df2[' <span style=\"color: #ff0000;\">year<\/span> ']. <span style=\"color: #3366ff;\">astype<\/span> (int)\n\n<span style=\"color: #008080;\">#merge two DataFrames\n<\/span>big_df = df1. <span style=\"color: #3366ff;\">merge<\/span> (df2, on=' <span style=\"color: #ff0000;\">year<\/span> ', how=' <span style=\"color: #ff0000;\">left<\/span> ')\n\n<span style=\"color: #008080;\">#view merged DataFrame\n<\/span>big_df\n\n\tyear sales refunds\n0 2015 500 31\n1 2016 534 36\n2 2017 564 40\n3 2018 671 40\n4 2019 700 43\n5 2020 840 70\n6 2021 810 62<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che non riceviamo alcun <strong>ValueError<\/strong> e riusciamo a unire con successo i due DataFrame in uno solo.<\/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 correggere altri errori comuni in Python:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/le-colonne-si-sovrappongono-ma-non-e-specificato-alcun-suffisso\/\" target=\"_blank\" rel=\"noopener\">Come risolvere il problema: le colonne si sovrappongono ma non \u00e8 specificato alcun suffisso<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/loggetto-numpy-ndarray-non-ha-attributi-aggiunti\/\" target=\"_blank\" rel=\"noopener\">Come risolvere il problema: l&#8217;oggetto &#8220;numpy.ndarray&#8221; non ha un attributo &#8220;append&#8221;.<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/errore-di-valore-se-si-utilizzano-tutti-i-valori-scalari-e-necessario-passare-un-indice\/\" target=\"_blank\" rel=\"noopener\">Come risolvere il problema: se utilizzi tutti i valori scalari, devi passare un indice<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Un errore che potresti riscontrare quando usi i panda \u00e8: ValueError : You are trying to merge on int64 and object columns. If you wish to proceed you should use pd.concat Questo errore si verifica quando si tenta di unire due DataFrame panda ma la colonna su cui si sta unendo \u00e8 un oggetto in [&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 risolvere il problema: stai tentando di unire le colonne object e int64 - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come correggere il seguente errore in Panda: &quot;stai tentando di unire le colonne oggetto e int64&quot;.\" \/>\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\/stai-tentando-di-unire-le-colonne-object-e-int64\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come risolvere il problema: stai tentando di unire le colonne object e int64 - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come correggere il seguente errore in Panda: &quot;stai tentando di unire le colonne oggetto e int64&quot;.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-20T07:55:51+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\/stai-tentando-di-unire-le-colonne-object-e-int64\/\",\"url\":\"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/\",\"name\":\"Come risolvere il problema: stai tentando di unire le colonne object e int64 - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-20T07:55:51+00:00\",\"dateModified\":\"2023-07-20T07:55:51+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come correggere il seguente errore in Panda: &quot;stai tentando di unire le colonne oggetto e int64&quot;.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come risolvere il problema: stai tentando di unire le colonne object e int64\"}]},{\"@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 risolvere il problema: stai tentando di unire le colonne object e int64 - Statorials","description":"Questo tutorial spiega come correggere il seguente errore in Panda: &quot;stai tentando di unire le colonne oggetto e int64&quot;.","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\/stai-tentando-di-unire-le-colonne-object-e-int64\/","og_locale":"it_IT","og_type":"article","og_title":"Come risolvere il problema: stai tentando di unire le colonne object e int64 - Statorials","og_description":"Questo tutorial spiega come correggere il seguente errore in Panda: &quot;stai tentando di unire le colonne oggetto e int64&quot;.","og_url":"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/","og_site_name":"Statorials","article_published_time":"2023-07-20T07:55:51+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\/stai-tentando-di-unire-le-colonne-object-e-int64\/","url":"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/","name":"Come risolvere il problema: stai tentando di unire le colonne object e int64 - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-20T07:55:51+00:00","dateModified":"2023-07-20T07:55:51+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come correggere il seguente errore in Panda: &quot;stai tentando di unire le colonne oggetto e int64&quot;.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/stai-tentando-di-unire-le-colonne-object-e-int64\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come risolvere il problema: stai tentando di unire le colonne object e int64"}]},{"@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\/2872"}],"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=2872"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2872\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}