{"id":4110,"date":"2023-07-13T13:10:59","date_gmt":"2023-07-13T13:10:59","guid":{"rendered":"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/"},"modified":"2023-07-13T13:10:59","modified_gmt":"2023-07-13T13:10:59","slug":"pands-extraheert-het-aantal-snaren","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/","title":{"rendered":"Hoe een getal uit een string te extraheren in pandas"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">U kunt de volgende basissyntaxis gebruiken om getallen uit een tekenreeks in panda&#8217;s te extraheren:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df[' <span style=\"color: #ff0000;\">my_column<\/span> ']. <span style=\"color: #3366ff;\">str<\/span> . <span style=\"color: #3366ff;\">extract<\/span> (' <span style=\"color: #ff0000;\">(\\d+)<\/span> ')\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Deze specifieke syntaxis extraheert de getallen uit elke tekenreeks in een kolom met de naam <strong>my_column<\/strong> in een pandas DataFrame.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Opmerking<\/strong> : bij gebruik van een reguliere expressie staat <strong>\\d<\/strong> voor &#8218;elk cijfer&#8216; en <strong>+<\/strong> voor &#8218;een of meer&#8216;.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Het volgende voorbeeld laat zien hoe u deze functie in de praktijk kunt gebruiken.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld: haal het getal uit een string in Pandas<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Stel dat we het volgende panda&#8217;s DataFrame hebben dat informatie bevat over de verkoop van verschillende producten:<\/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<span style=\"color: #000000;\">\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">product<\/span> ': ['A33', 'B34', 'A22', 'A50', 'C200', 'D7', '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 A33 18\n1 B34 22\n2 A22 19\n3 A50 14\n4 C200 14\n5 D7 11\n6 A9 20\n7 A13 28<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Stel dat we het nummer van elke string uit de <strong>productkolom<\/strong> willen halen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">We kunnen hiervoor de volgende syntaxis gebruiken:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#extract numbers from strings in 'product' column\n<\/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> ')\n\n\t0\n0 33\n1 34\n2 22\n3 50\n4,200\n5 7\n6 9\n7 13\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Het resultaat is een DataFrame dat alleen de getallen in elke rij van de <strong>Product-<\/strong> kolom bevat.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Bijvoorbeeld:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">De formule haalt <strong>33<\/strong> uit de tekenreeks <strong>A33<\/strong> op de eerste regel.<\/span><\/li>\n<li> <span style=\"color: #000000;\">De formule haalt <b>34<\/b> uit de tekenreeks <strong>B34<\/strong> op de eerste regel.<\/span><\/li>\n<li> <span style=\"color: #000000;\">De formule haalt <b>22<\/b> uit de tekenreeks <strong>A22<\/strong> in de eerste rij.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Enzovoort.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Als je wilt, kun je deze numerieke waarden ook opslaan in een nieuwe kolom van het 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;\">#extract numbers from strings in 'product' column and store them in new column\n<\/span>df[' <span style=\"color: #ff0000;\">product_numbers<\/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> ')\n\n<span style=\"color: #008080;\">#view updated DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  product sales product_numbers\n0 A33 18 33\n1 B34 22 34\n2 A22 19 22\n3 A50 14 50\n4 C200 14,200\n5 D7 11 7\n6 A9 20 9\n7 A13 28 13\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">De nieuwe kolom met de naam <strong>productnummers<\/strong> bevat alleen de getallen voor elke tekenreeks in de <strong>productkolom<\/strong> .<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Aanvullende bronnen<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">In de volgende tutorials wordt uitgelegd hoe u andere veelvoorkomende bewerkingen in panda&#8217;s kunt uitvoeren:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/pandas-gesorteerd-op-keten\/\" target=\"_blank\" rel=\"noopener\">Panda&#8217;s: Hoe DataFrame te sorteren op basis van een stringkolom<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/pandas-verwijdert-tekens-uit-de-tekenreeks\/\" target=\"_blank\" rel=\"noopener\">Panda&#8217;s: specifieke tekens uit tekenreeksen verwijderen<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/zoek-naar-string-in-pandas-dataframe\/\" target=\"_blank\" rel=\"noopener\">Panda&#8217;s: zoek naar een string in alle kolommen van DataFrame<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>U kunt de volgende basissyntaxis gebruiken om getallen uit een tekenreeks in panda&#8217;s te extraheren: df[&#8218; my_column &#8218;]. str . extract (&#8218; (\\d+) &#8218;) Deze specifieke syntaxis extraheert de getallen uit elke tekenreeks in een kolom met de naam my_column in een pandas DataFrame. Opmerking : bij gebruik van een reguliere expressie staat \\d voor [&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":[],"class_list":["post-4110","post","type-post","status-publish","format-standard","hentry","category-gids"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Hoe een getal uit een string te extraheren in Pandas - Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u getallen uit tekenreeksen in panda&#039;s kunt extraheren.\" \/>\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\/nl\/pands-extraheert-het-aantal-snaren\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe een getal uit een string te extraheren in Pandas - Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u getallen uit tekenreeksen in panda&#039;s kunt extraheren.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-13T13:10:59+00:00\" \/>\n<meta name=\"author\" content=\"Dr.benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Verfasst von\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr.benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"2\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/\",\"url\":\"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/\",\"name\":\"Hoe een getal uit een string te extraheren in Pandas - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-13T13:10:59+00:00\",\"dateModified\":\"2023-07-13T13:10:59+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u getallen uit tekenreeksen in panda&#39;s kunt extraheren.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoe een getal uit een string te extraheren in pandas\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/nl\/#website\",\"url\":\"https:\/\/statorials.org\/nl\/\",\"name\":\"Statorials\",\"description\":\"Uw gids voor statistische competentie\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/nl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"de\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\",\"name\":\"Dr.benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr.benjamin anderson\"},\"description\":\"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder\",\"sameAs\":[\"http:\/\/statorials.org\/nl\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hoe een getal uit een string te extraheren in Pandas - Statorials","description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u getallen uit tekenreeksen in panda&#39;s kunt extraheren.","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\/nl\/pands-extraheert-het-aantal-snaren\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe een getal uit een string te extraheren in Pandas - Statorials","og_description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u getallen uit tekenreeksen in panda&#39;s kunt extraheren.","og_url":"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/","og_site_name":"Statorials","article_published_time":"2023-07-13T13:10:59+00:00","author":"Dr.benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr.benjamin anderson","Gesch\u00e4tzte Lesezeit":"2\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/","url":"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/","name":"Hoe een getal uit een string te extraheren in Pandas - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-13T13:10:59+00:00","dateModified":"2023-07-13T13:10:59+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u getallen uit tekenreeksen in panda&#39;s kunt extraheren.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/pands-extraheert-het-aantal-snaren\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Hoe een getal uit een string te extraheren in pandas"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/nl\/#website","url":"https:\/\/statorials.org\/nl\/","name":"Statorials","description":"Uw gids voor statistische competentie","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/nl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"de"},{"@type":"Person","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219","name":"Dr.benjamin anderson","image":{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/","url":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr.benjamin anderson"},"description":"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder","sameAs":["http:\/\/statorials.org\/nl"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/4110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/comments?post=4110"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/4110\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=4110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=4110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=4110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}