{"id":3608,"date":"2023-07-16T14:02:09","date_gmt":"2023-07-16T14:02:09","guid":{"rendered":"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/"},"modified":"2023-07-16T14:02:09","modified_gmt":"2023-07-16T14:02:09","slug":"pandas-creeren-een-categorische-variabele","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/","title":{"rendered":"Categorische variabelen maken in panda&#39;s (met voorbeelden)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">U kunt een van de volgende methoden gebruiken om een<a href=\"https:\/\/statorials.org\/nl\/categorisch-versus-kwantitatief\/\" target=\"_blank\" rel=\"noopener\">categorische variabele<\/a> in panda&#8217;s te maken:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Methode 1: Cre\u00eber een categorische variabele vanaf nul<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df[' <span style=\"color: #ff0000;\">cat_variable<\/span> '] = [' <span style=\"color: #ff0000;\">A<\/span> ', ' <span style=\"color: #ff0000;\">B<\/span> ', ' <span style=\"color: #ff0000;\">C<\/span> ', ' <span style=\"color: #ff0000;\">D<\/span> ']\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Methode 2: Cre\u00eber een categorische variabele op basis van een bestaande numerieke variabele<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df[' <span style=\"color: #ff0000;\">cat_variable<\/span> '] = pd. <span style=\"color: #3366ff;\">cut<\/span> (df[' <span style=\"color: #ff0000;\">numeric_variable<\/span> '],\n                            bins=[ <span style=\"color: #008000;\">0,15,25<\/span> ,float(' <span style=\"color: #ff0000;\">Inf<\/span> ') <span style=\"color: #008000;\">]<\/span> <span style=\"color: #008000;\">,<\/span>\n                            labels=[' <span style=\"color: #ff0000;\">Bad<\/span> ', ' <span style=\"color: #ff0000;\">OK<\/span> ', ' <span style=\"color: #ff0000;\">Good<\/span> '])<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">De volgende voorbeelden laten zien hoe u elke methode in de praktijk kunt gebruiken.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld 1: Cre\u00eber een categorische variabele vanaf nul<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u een Panda DataFrame maakt met een categorische variabele met de naam <strong>team<\/strong> en een numerieke variabele met de naam <strong>punten<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#create DataFrame with one categorical variable and one numeric variable\n<\/span>df = 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> ': [12, 15, 19, 22, 24, 25, 26, 30]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  team points\n0 to 12\n1 B 15\n2 C 19\n3 D 22\n4 E 24\n5 F 25\n6 G 26\n7:30 a.m.\n\n<span style=\"color: #008080;\">#view data type of each column in DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> ( <span style=\"color: #3366ff;\">df.dtypes<\/span> )\n\nteam object\nint64 dots\ndtype:object<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Met <strong>df.dtypes<\/strong> kunnen we het <a href=\"https:\/\/statorials.org\/nl\/pandas-controleren-dtype-alle-kolommen\/\" target=\"_blank\" rel=\"noopener\">gegevenstype van elke variabele<\/a> in het DataFrame zien.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Wij kunnen zien:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">De teamvariabele is een <strong>object<\/strong> .<\/span><\/li>\n<li> <span style=\"color: #000000;\">De puntenvariabele is een <strong>geheel getal<\/strong> .<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">In Python is een <strong>object<\/strong> gelijk aan een teken of een \u2018categorische\u2019 variabele. De teamvariabele is dus een categorische variabele.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld 2: Maak een categorische variabele op basis van een bestaande numerieke variabele<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u een categorische variabele met de naam <strong>status<\/strong> kunt maken op basis van de bestaande numerieke variabele met de naam <strong>punten<\/strong> in 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: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#create DataFrame with one categorical variable and one numeric variable\n<\/span>df = 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> ': [12, 15, 19, 22, 24, 25, 26, 30]})\n\n<span style=\"color: #008080;\">#create categorical variable 'status' based on existing numerical 'points' variable\n<\/span>df[' <span style=\"color: #ff0000;\">status<\/span> '] = pd. <span style=\"color: #3366ff;\">cut<\/span> (df[' <span style=\"color: #ff0000;\">points<\/span> '],\n                      bins=[ <span style=\"color: #008000;\">0,15,25<\/span> ,float(' <span style=\"color: #ff0000;\">Inf<\/span> ') <span style=\"color: #008000;\">]<\/span> <span style=\"color: #008000;\">,<\/span>\n                      labels=[' <span style=\"color: #ff0000;\">Bad<\/span> ', ' <span style=\"color: #ff0000;\">OK<\/span> ', ' <span style=\"color: #ff0000;\">Good<\/span> '])\n\n<span style=\"color: #008080;\">#view updated DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n  team points status\n0 To 12 Bad\n1 B 15 Bad\n2 C 19 OK\n3 D 22 OK\n4 E 24 OK\n5 F 25 OK\n6 G 26 Good\n7:30 a.m. Good<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Met behulp van de functie <strong>cut()<\/strong> hebben we een nieuwe categorische variabele gemaakt met de naam <strong>status<\/strong> , die de volgende waarden aanneemt:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">&#8218; <strong>Slecht<\/strong> &#8218; als de waarde in de puntenkolom kleiner is dan of gelijk is aan 15.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Anders &#8218; <strong>OK<\/strong> &#8218; als de waarde in de puntenkolom kleiner is dan of gelijk is aan 25.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Anders: \u201c <strong>Goed<\/strong> \u201d.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Houd er rekening mee dat bij gebruik van de<\/span> functie <strong>cut()<\/strong> <span style=\"color: #000000;\">het aantal<\/span> <strong>labels<\/strong> <span style=\"color: #000000;\">\u00e9\u00e9n minder moet zijn dan het aantal<\/span> <strong>bakken<\/strong> <span style=\"color: #000000;\">.<\/span><\/span><\/p>\n<p> <span style=\"color: #000000;\">In ons voorbeeld hebben we vier waarden voor <strong>vakken<\/strong> gebruikt om de randen van de vakken te defini\u00ebren en drie waarden voor <strong>labels<\/strong> om de labels op te geven die voor de categorische variabele moeten worden gebruikt.<\/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 panda-taken kunt uitvoeren:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/pandas-worden-modellen\/\" target=\"_blank\" rel=\"noopener\">Hoe dummyvariabelen in Panda&#8217;s te maken<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/converteer-categorische-variabele-naar-digitale-pandas\/\" target=\"_blank\" rel=\"noopener\">Hoe een categorische variabele naar numeriek te converteren in Pandas<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/pandas-converteren-boolean-naar-int\/\" target=\"_blank\" rel=\"noopener\">Hoe Booleaanse waarden naar gehele waarden in Panda&#8217;s te converteren<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>U kunt een van de volgende methoden gebruiken om eencategorische variabele in panda&#8217;s te maken: Methode 1: Cre\u00eber een categorische variabele vanaf nul df[&#8218; cat_variable &#8218;] = [&#8218; A &#8218;, &#8218; B &#8218;, &#8218; C &#8218;, &#8218; D &#8218;] Methode 2: Cre\u00eber een categorische variabele op basis van een bestaande numerieke variabele df[&#8218; cat_variable &#8218;] [&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-3608","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>Categorische variabelen maken in panda&#039;s (met voorbeelden) - Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt uitgelegd hoe je categorische variabelen in panda&#039;s maakt, met verschillende voorbeelden.\" \/>\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\/pandas-creeren-een-categorische-variabele\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Categorische variabelen maken in panda&#039;s (met voorbeelden) - Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt uitgelegd hoe je categorische variabelen in panda&#039;s maakt, met verschillende voorbeelden.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-16T14:02:09+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=\"3\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/\",\"url\":\"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/\",\"name\":\"Categorische variabelen maken in panda&#39;s (met voorbeelden) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-16T14:02:09+00:00\",\"dateModified\":\"2023-07-16T14:02:09+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt uitgelegd hoe je categorische variabelen in panda&#39;s maakt, met verschillende voorbeelden.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Categorische variabelen maken in panda&#39;s (met voorbeelden)\"}]},{\"@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":"Categorische variabelen maken in panda&#39;s (met voorbeelden) - Statorials","description":"In deze tutorial wordt uitgelegd hoe je categorische variabelen in panda&#39;s maakt, met verschillende voorbeelden.","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\/pandas-creeren-een-categorische-variabele\/","og_locale":"de_DE","og_type":"article","og_title":"Categorische variabelen maken in panda&#39;s (met voorbeelden) - Statorials","og_description":"In deze tutorial wordt uitgelegd hoe je categorische variabelen in panda&#39;s maakt, met verschillende voorbeelden.","og_url":"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/","og_site_name":"Statorials","article_published_time":"2023-07-16T14:02:09+00:00","author":"Dr.benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr.benjamin anderson","Gesch\u00e4tzte Lesezeit":"3\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/","url":"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/","name":"Categorische variabelen maken in panda&#39;s (met voorbeelden) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-16T14:02:09+00:00","dateModified":"2023-07-16T14:02:09+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt uitgelegd hoe je categorische variabelen in panda&#39;s maakt, met verschillende voorbeelden.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/pandas-creeren-een-categorische-variabele\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Categorische variabelen maken in panda&#39;s (met voorbeelden)"}]},{"@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\/3608","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=3608"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3608\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=3608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=3608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=3608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}