{"id":3606,"date":"2023-07-16T14:02:09","date_gmt":"2023-07-16T14:02:09","guid":{"rendered":"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/"},"modified":"2023-07-16T14:02:09","modified_gmt":"2023-07-16T14:02:09","slug":"pandas-erstellen-kategoriale-variablen","status":"publish","type":"post","link":"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/","title":{"rendered":"So erstellen sie kategoriale variablen in pandas (mit beispielen)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Sie k\u00f6nnen eine der folgenden Methoden verwenden, um eine <a href=\"https:\/\/statorials.org\/de\/kategorisch-vs.-quantitativ\/\" target=\"_blank\" rel=\"noopener\">kategoriale Variable<\/a> in Pandas zu erstellen:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Methode 1: Erstellen Sie eine kategoriale Variable von Grund auf<\/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: Erstellen Sie eine kategoriale Variable aus einer vorhandenen numerischen Variablen<\/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;\">Die folgenden Beispiele zeigen, wie die einzelnen Methoden in der Praxis angewendet werden.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Beispiel 1: Erstellen Sie eine kategoriale Variable von Grund auf<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Der folgende Code zeigt, wie man einen Pandas-DataFrame mit einer kategorialen Variablen namens \u201e <strong>team<\/strong> \u201c und einer numerischen Variablen namens \u201e <strong>points\u201c<\/strong> erstellt:<\/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;\">Mit <strong>df.dtypes<\/strong> k\u00f6nnen wir den <a href=\"https:\/\/statorials.org\/de\/pandas-uberprufen-den-dtype-aller-spalten\/\" target=\"_blank\" rel=\"noopener\">Datentyp jeder Variablen<\/a> im DataFrame sehen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Wir sehen:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Die Teamvariable ist ein <strong>Objekt<\/strong> .<\/span><\/li>\n<li> <span style=\"color: #000000;\">Die Punktevariable ist eine <strong>Ganzzahl<\/strong> .<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">In Python entspricht ein <strong>Objekt<\/strong> einem Zeichen oder einer \u201ekategorialen\u201c Variablen. Somit ist die Teamvariable eine kategoriale Variable.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Beispiel 2: Erstellen Sie eine kategoriale Variable aus einer vorhandenen numerischen Variablen<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Der folgende Code zeigt, wie man aus der vorhandenen numerischen Variablen namens \u201e <strong>points\u201c<\/strong> im DataFrame eine kategoriale Variable namens \u201e <strong>status\u201c<\/strong> erstellt:<\/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;\">Mit der Funktion <strong>\u201ecut()\u201c<\/strong> haben wir eine neue kategoriale Variable namens <strong>\u201estatus\u201c<\/strong> erstellt, die die folgenden Werte annimmt:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">\u201e <strong>Schlecht<\/strong> \u201c, wenn der Wert in der Punktespalte kleiner oder gleich 15 ist.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Andernfalls \u201e <strong>OK<\/strong> \u201c, wenn der Wert in der Punktespalte kleiner oder gleich 25 ist.<\/span><\/li>\n<li> <span style=\"color: #000000;\">Ansonsten \u201e <strong>Gut<\/strong> \u201c.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Beachten Sie, dass bei Verwendung der<\/span> Funktion <strong>\u201ecut()\u201c<\/strong> <span style=\"color: #000000;\">die Anzahl der<\/span> <strong>Etiketten<\/strong> <span style=\"color: #000000;\">um eins kleiner sein muss als die Anzahl der<\/span> <strong>Beh\u00e4lter<\/strong> <span style=\"color: #000000;\">.<\/span><\/span><\/p>\n<p> <span style=\"color: #000000;\">In unserem Beispiel haben wir vier Werte f\u00fcr <strong>Boxen<\/strong> verwendet, um die R\u00e4nder der Boxen zu definieren, und drei Werte f\u00fcr <strong>Beschriftungen<\/strong> , um die Beschriftungen anzugeben, die f\u00fcr die kategoriale Variable verwendet werden sollen.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Zus\u00e4tzliche Ressourcen<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">In den folgenden Tutorials wird erl\u00e4utert, wie Sie andere h\u00e4ufige Panda-Aufgaben ausf\u00fchren:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/de\/pandas-werden-zu-models\/\" target=\"_blank\" rel=\"noopener\">So erstellen Sie Dummy-Variablen in Pandas<\/a><br \/> <a href=\"https:\/\/statorials.org\/de\/konvertieren-sie-kategoriale-variablen-in-digitale-pandas\/\" target=\"_blank\" rel=\"noopener\">So konvertieren Sie eine kategoriale Variable in Pandas in eine numerische Variable<\/a><br \/> <a href=\"https:\/\/statorials.org\/de\/pandas-konvertieren-booleschen-wert-in-int\/\" target=\"_blank\" rel=\"noopener\">So konvertieren Sie boolesche Werte in Pandas in ganzzahlige Werte<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sie k\u00f6nnen eine der folgenden Methoden verwenden, um eine kategoriale Variable in Pandas zu erstellen: Methode 1: Erstellen Sie eine kategoriale Variable von Grund auf df[&#8218; cat_variable &#8218;] = [&#8218; A &#8218;, &#8218; B &#8218;, &#8218; C &#8218;, &#8218; D &#8218;] Methode 2: Erstellen Sie eine kategoriale Variable aus einer vorhandenen numerischen Variablen df[&#8218; cat_variable [&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>So erstellen Sie kategoriale Variablen in Pandas (mit Beispielen) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"In diesem Tutorial wird anhand mehrerer Beispiele erl\u00e4utert, wie kategoriale Variablen in Pandas erstellt werden.\" \/>\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\/de\/pandas-erstellen-kategoriale-variablen\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"So erstellen Sie kategoriale Variablen in Pandas (mit Beispielen) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"In diesem Tutorial wird anhand mehrerer Beispiele erl\u00e4utert, wie kategoriale Variablen in Pandas erstellt werden.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/\" \/>\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 Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/\",\"url\":\"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/\",\"name\":\"So erstellen Sie kategoriale Variablen in Pandas (mit Beispielen) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/de\/#website\"},\"datePublished\":\"2023-07-16T14:02:09+00:00\",\"dateModified\":\"2023-07-16T14:02:09+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0\"},\"description\":\"In diesem Tutorial wird anhand mehrerer Beispiele erl\u00e4utert, wie kategoriale Variablen in Pandas erstellt werden.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/#breadcrumb\"},\"inLanguage\":\"de-DE\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Heim\",\"item\":\"https:\/\/statorials.org\/de\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"So erstellen sie kategoriale variablen in pandas (mit beispielen)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/de\/#website\",\"url\":\"https:\/\/statorials.org\/de\/\",\"name\":\"Statorials\",\"description\":\"Ihr Leitfaden f\u00fcr statistische Kompetenz !\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/de\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"de-DE\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0\",\"name\":\"Dr. Benjamin Anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de-DE\",\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. Benjamin Anderson\"},\"description\":\"Hallo, ich bin Benjamin, ein pensionierter Statistikprofessor, der sich zum engagierten Statorials-Lehrer entwickelt hat. Mit umfassender Erfahrung und Fachwissen auf dem Gebiet der Statistik bin ich bestrebt, mein Wissen zu teilen, um Studenten durch Statorials zu bef\u00e4higen. Mehr wissen\",\"sameAs\":[\"https:\/\/statorials.org\/de\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"So erstellen Sie kategoriale Variablen in Pandas (mit Beispielen) \u2013 Statorials","description":"In diesem Tutorial wird anhand mehrerer Beispiele erl\u00e4utert, wie kategoriale Variablen in Pandas erstellt werden.","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\/de\/pandas-erstellen-kategoriale-variablen\/","og_locale":"de_DE","og_type":"article","og_title":"So erstellen Sie kategoriale Variablen in Pandas (mit Beispielen) \u2013 Statorials","og_description":"In diesem Tutorial wird anhand mehrerer Beispiele erl\u00e4utert, wie kategoriale Variablen in Pandas erstellt werden.","og_url":"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/","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 Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/","url":"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/","name":"So erstellen Sie kategoriale Variablen in Pandas (mit Beispielen) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/de\/#website"},"datePublished":"2023-07-16T14:02:09+00:00","dateModified":"2023-07-16T14:02:09+00:00","author":{"@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0"},"description":"In diesem Tutorial wird anhand mehrerer Beispiele erl\u00e4utert, wie kategoriale Variablen in Pandas erstellt werden.","breadcrumb":{"@id":"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/#breadcrumb"},"inLanguage":"de-DE","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/de\/pandas-erstellen-kategoriale-variablen\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Heim","item":"https:\/\/statorials.org\/de\/"},{"@type":"ListItem","position":2,"name":"So erstellen sie kategoriale variablen in pandas (mit beispielen)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/de\/#website","url":"https:\/\/statorials.org\/de\/","name":"Statorials","description":"Ihr Leitfaden f\u00fcr statistische Kompetenz !","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/de\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"de-DE"},{"@type":"Person","@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0","name":"Dr. Benjamin Anderson","image":{"@type":"ImageObject","inLanguage":"de-DE","@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg","caption":"Dr. Benjamin Anderson"},"description":"Hallo, ich bin Benjamin, ein pensionierter Statistikprofessor, der sich zum engagierten Statorials-Lehrer entwickelt hat. Mit umfassender Erfahrung und Fachwissen auf dem Gebiet der Statistik bin ich bestrebt, mein Wissen zu teilen, um Studenten durch Statorials zu bef\u00e4higen. Mehr wissen","sameAs":["https:\/\/statorials.org\/de"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts\/3606"}],"collection":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/comments?post=3606"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts\/3606\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/media?parent=3606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/categories?post=3606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/tags?post=3606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}