{"id":10429,"date":"2020-11-17T18:47:00","date_gmt":"2020-11-17T18:47:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=10429"},"modified":"2020-11-17T18:47:01","modified_gmt":"2020-11-17T18:47:01","slug":"print-column-names-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/pandas\/print-column-names-in-python","title":{"rendered":"3 Easy Ways to Print column Names in Python"},"content":{"rendered":"\n<p>Hello, readers! In this article, we will be focusing on different <strong>ways to print column names<\/strong> in Python.<\/p>\n\n\n\n<p>So, let us get started!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">First, where do you find columns in Python?<\/h2>\n\n\n\n<p>We often come across questions and problem statements wherein we feel the need to deal with data in an <strong>excel <\/strong>or <strong>csv <\/strong>file i.e. in the form of rows and columns.<\/p>\n\n\n\n<p><a aria-label=\"Python (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"https:\/\/www.askpython.com\/python\" target=\"_blank\" class=\"rank-math-link\">Python<\/a>, as a programming language, provides us with a data structure called &#8216;DataFrame&#8217; to deal with rows and columns.<\/p>\n\n\n\n<p>A <strong><a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/dataframes-in-python\" class=\"rank-math-link\">Python DataFrame<\/a><\/strong> consists of rows and columns and the <a aria-label=\"Pandas module (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" target=\"_blank\" class=\"rank-math-link\">Pandas module<\/a> offers us various functions to manipulate and deal with the data occupied within these rows and columns.<\/p>\n\n\n\n<p>Today, we will be having a look at the various different ways through which we can fetch and display the column header\/names of a dataframe or a csv file.<\/p>\n\n\n\n<p>We would be referring the below csv file in the below examples&#8211;<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"740\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/11\/Dataset-bank_loan-1024x740.png\" alt=\"Dataset-Bank Loan - print column names in Python\" class=\"wp-image-10451\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/11\/Dataset-bank_loan-1024x740.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/11\/Dataset-bank_loan-300x217.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/11\/Dataset-bank_loan-768x555.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/11\/Dataset-bank_loan.png 1101w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption><strong>Dataset-Bank Loan<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Using pandas.dataframe.columns to print column names in Python<\/h2>\n\n\n\n<p>We can use <code>pandas.dataframe.columns<\/code> variable to print the column tags or headers at ease. Have a look at the below syntax!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndata.columns\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas\n\nfile = pandas.read_csv(&quot;D:\/Edwisor_Project - Loan_Defaulter\/bank-loan.csv&quot;)\nfor col in file.columns:\n    print(col)\n<\/pre><\/div>\n\n\n<p>In this example, we have loaded the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-csv-module\" class=\"rank-math-link\">csv file<\/a> into the environment. Further, we have printed the column names through a for loop using dataframe.columns variable.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nage\ned\nemploy\naddress\nincome\ndebtinc\ncreddebt\nothdebt\ndefault\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Using pandas.dataframe.columns.values<\/h2>\n\n\n\n<p>Python provides us with <code>pandas.dataframe.columns.values<\/code> to extract the column names from the dataframe or csv file and print them.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndata.columns.values\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas\n\nfile = pandas.read_csv(&quot;D:\/Edwisor_Project - Loan_Defaulter\/bank-loan.csv&quot;)\nprint(file.columns.values)\n<\/pre><\/div>\n\n\n<p>So, the data.columns.values gives us a <a aria-label=\"list  (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">list<\/a> of column names\/headers present in the dataframe.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;age&#039; &#039;ed&#039; &#039;employ&#039; &#039;address&#039; &#039;income&#039; &#039;debtinc&#039; &#039;creddebt&#039; &#039;othdebt&#039; &#039;default&#039;]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Python sorted() method to get the column names<\/h2>\n\n\n\n<p>Python <code>sorted()<\/code> method can be used to get the list of  column names of a dataframe in an <strong>ascending<\/strong> <strong>order <\/strong>of columns.<\/p>\n\n\n\n<p>Have a look at the below syntax!<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsorted(dataframe)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas\n\nfile = pandas.read_csv(&quot;D:\/Edwisor_Project - Loan_Defaulter\/bank-loan.csv&quot;)\nprint(sorted(file))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;address&#039;, &#039;age&#039;, &#039;creddebt&#039;, &#039;debtinc&#039;, &#039;default&#039;, &#039;ed&#039;, &#039;employ&#039;, &#039;income&#039;, &#039;othdebt&#039;]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of this topic. Hope this article turns out to be a hack for you in terms of different solutions for a single problem statement.<\/p>\n\n\n\n<p>For more such posts related to Python, Stay tuned and till then, Happy Learning!! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, readers! In this article, we will be focusing on different ways to print column names in Python. So, let us get started! First, where do you find columns in Python? We often come across questions and problem statements wherein we feel the need to deal with data in an excel or csv file i.e. [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":10453,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94],"tags":[],"class_list":["post-10429","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pandas"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/10429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=10429"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/10429\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/10453"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=10429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=10429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=10429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}