{"id":3701,"date":"2023-07-16T01:36:44","date_gmt":"2023-07-16T01:36:44","guid":{"rendered":"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/"},"modified":"2023-07-16T01:36:44","modified_gmt":"2023-07-16T01:36:44","slug":"i-panda-sottraggono-due-dataframe","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/","title":{"rendered":"Panda: come sottrarre due dataframes"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi per sottrarre un DataFrame panda da un altro:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df1. <span style=\"color: #3366ff;\">subtract<\/span> (df2)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Se hai una colonna di caratteri in ciascun DataFrame, potrebbe essere necessario spostarla prima nella colonna dell&#8217;indice di ciascun DataFrame:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df1. <span style=\"color: #3366ff;\">set_index<\/span> (' <span style=\"color: #ff0000;\">char_column<\/span> '). <span style=\"color: #3366ff;\">subtract<\/span> ( <span style=\"color: #3366ff;\">df2.set_index<\/span> (' <span style=\"color: #ff0000;\">char_column<\/span> '))<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come utilizzare nella pratica ciascuna sintassi.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 1: sottrarre due dataframe Pandas (solo colonne numeriche)<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Diciamo che abbiamo i seguenti due DataFrames panda che hanno solo colonne numeriche:<\/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: #000000;\"><span style=\"color: #008080;\">\n<span style=\"color: #000000;\"><span style=\"color: #008080;\">#create first DataFrame\n<\/span>df1 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">points<\/span> ': [5, 17, 7, 19, 12, 13, 9, 24],\n                    ' <span style=\"color: #ff0000;\">assists<\/span> ': [4, 7, 7, 6, 8, 7, 10, 11]})\n\n<span style=\"color: #008000;\">print<\/span> (df1)\n\n   assist points\n0 5 4\n1 17 7\n2 7 7\n3 19 6\n4 12 8\n5 13 7\n6 9 10\n7 24 11\n\n<span style=\"color: #008080;\">#create second DataFrame\n<\/span>df2 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">points<\/span> ': [4, 22, 10, 3, 7, 8, 12, 10],\n                    ' <span style=\"color: #ff0000;\">assists<\/span> ': [3, 5, 5, 4, 7, 14, 9, 5]})\n\n<span style=\"color: #008000;\">print<\/span> (df2)\n\n   assist points\n0 4 3\n1 22 5\n2 10 5\n3 3 4\n4 7 7\n5 8 14\n6 12 9\n7 10 5\n<\/span><\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come sottrarre i valori corrispondenti tra i due DataFrame:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#subtract corresponding values between the two DataFrames<\/span>\ndf1. <span style=\"color: #3366ff;\">subtract<\/span> (df2)\n\n\tassist points\n0 1 1\n1 -5 2\n2 -3 2\n3 16 2\n4 5 1\n5 5 -7\n6 -3 1\n7 14 6\n<\/strong><\/span><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 2: sottrarre due Pandas DataFrames (miscela di colonne di caratteri e numeriche)<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere i seguenti due DataFrames panda, ciascuno con una colonna di caratteri chiamata <strong>team<\/strong> :<\/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: #000000;\"><span style=\"color: #008080;\">\n#create first DataFrame\n<\/span>df1 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],\n                    ' <span style=\"color: #ff0000;\">points<\/span> ': [5, 17, 7, 19, 12, 13, 9, 24],\n                    ' <span style=\"color: #ff0000;\">assists<\/span> ': [4, 7, 7, 6, 8, 7, 10, 11]})\n\n<span style=\"color: #008000;\">print<\/span> (df1)\n\n  team points assists\n0 to 5 4\n1 B 17 7\n2 C 7 7\n3 D 19 6\n4 E 12 8\n5 F 13 7\n6 G 9 10\n7:24 a.m. 11\n\n<span style=\"color: #008080;\">#create second DataFrame\n<\/span>df2 = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">team<\/span> ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],\n                    ' <span style=\"color: #ff0000;\">points<\/span> ': [4, 22, 10, 3, 7, 8, 12, 10],\n                    ' <span style=\"color: #ff0000;\">assists<\/span> ': [3, 5, 5, 4, 7, 14, 9, 3]})\n\n<span style=\"color: #008000;\">print<\/span> (df2)\n\n  team points assists\n0 to 4 3\n1 B 22 5\n2 C 10 5\n3 D 3 4\n4 E 7 7\n5 F 8 14\n6 G 12 9\n7:10 a.m. 3<\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come spostare la colonna <strong>del team<\/strong> nella colonna dell&#8217;indice di ciascun DataFrame, quindi sottrarre i valori corrispondenti tra i due DataFrame:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #000000;\"><span style=\"color: #008080;\">#move 'team' column to index of each DataFrame and subtract corresponding values\n<\/span>df1. <span style=\"color: #3366ff;\">set_index<\/span> (' <span style=\"color: #ff0000;\">team<\/span> '). <span style=\"color: #3366ff;\">subtract<\/span> ( <span style=\"color: #3366ff;\">df2.set_index<\/span> (' <span style=\"color: #ff0000;\">team<\/span> '))\n\n\tassist points\nteam\t\t\nAt 1 1\nB-52\nC -3 2\nD 16 2\nE 5 1\nF 5 -7\nG -3 1\nH 14 8\n<\/span><\/strong><\/pre>\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\/differenza-dei-panda-tra-due-colonne\/\" target=\"_blank\" rel=\"noopener\">Panda: come trovare la differenza tra due colonne<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/differenza-dei-panda-tra-le-righe\/\" target=\"_blank\" rel=\"noopener\">Panda: come trovare la differenza tra due linee<\/a><br \/><a href=\"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-colonne\/\" target=\"_blank\" rel=\"noopener\">Panda: come sottrarre due colonne<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi per sottrarre un DataFrame panda da un altro: df1. subtract (df2) Se hai una colonna di caratteri in ciascun DataFrame, potrebbe essere necessario spostarla prima nella colonna dell&#8217;indice di ciascun DataFrame: df1. set_index (&#8216; char_column &#8216;). subtract ( df2.set_index (&#8216; char_column &#8216;)) Gli esempi seguenti mostrano come utilizzare nella [&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 sottrarre due DataFrames - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come sottrarre valori tra due DataFrames panda, con diversi esempi.\" \/>\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-sottraggono-due-dataframe\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Panda: come sottrarre due DataFrames - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come sottrarre valori tra due DataFrames panda, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-16T01:36:44+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-sottraggono-due-dataframe\/\",\"url\":\"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/\",\"name\":\"Panda: come sottrarre due DataFrames - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-16T01:36:44+00:00\",\"dateModified\":\"2023-07-16T01:36:44+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come sottrarre valori tra due DataFrames panda, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Panda: come sottrarre due dataframes\"}]},{\"@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 sottrarre due DataFrames - Statorials","description":"Questo tutorial spiega come sottrarre valori tra due DataFrames panda, con diversi esempi.","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-sottraggono-due-dataframe\/","og_locale":"it_IT","og_type":"article","og_title":"Panda: come sottrarre due DataFrames - Statorials","og_description":"Questo tutorial spiega come sottrarre valori tra due DataFrames panda, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/","og_site_name":"Statorials","article_published_time":"2023-07-16T01:36:44+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-sottraggono-due-dataframe\/","url":"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/","name":"Panda: come sottrarre due DataFrames - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-16T01:36:44+00:00","dateModified":"2023-07-16T01:36:44+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come sottrarre valori tra due DataFrames panda, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/i-panda-sottraggono-due-dataframe\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Panda: come sottrarre due dataframes"}]},{"@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\/3701"}],"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=3701"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3701\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}