How do you print in columns in python?
Hello, readers! In this article, we will be focusing on different ways to print column names in Python. So, let us get started! 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. in the form of rows and columns. Python, as a programming language, provides us with a data structure called ‘DataFrame’ to deal with rows and columns. A Python DataFrame consists of rows and columns and the Pandas module offers us various functions to manipulate and deal with the data occupied within these rows and columns. 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. We would be referring the below csv
file in the below examples– 1. Using pandas.dataframe.columns to print column names in PythonWe can use Example: import pandas file = pandas.read_csv("D:/Edwisor_Project - Loan_Defaulter/bank-loan.csv") for col in file.columns: print(col) In this example, we have loaded the csv file into the environment. Further, we have printed the column names through a for loop using dataframe.columns variable. Output: age ed employ address income debtinc creddebt othdebt default 2. Using pandas.dataframe.columns.valuesPython provides us with Syntax: Example: import pandas file = pandas.read_csv("D:/Edwisor_Project - Loan_Defaulter/bank-loan.csv") print(file.columns.values) So, the data.columns.values gives us a list of column names/headers present in the dataframe. Output: ['age' 'ed' 'employ' 'address' 'income' 'debtinc' 'creddebt' 'othdebt' 'default'] 3. Python sorted() method to get the column namesPython Have a look at the below syntax! Syntax: Example: import pandas file = pandas.read_csv("D:/Edwisor_Project - Loan_Defaulter/bank-loan.csv") print(sorted(file)) Output: ['address', 'age', 'creddebt', 'debtinc', 'default', 'ed', 'employ', 'income', 'othdebt'] ConclusionBy 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. For more such posts related to Python, Stay tuned and till then, Happy Learning!! 🙂 To get fancier tables like
you can use this Python recipe:
The Python recipe page contains a few improvements on it. How do I print specific rows and columns in Python?You can use the df. loc[[2]] to print a specific row of a pandas dataframe.
How do I print a column in Pandas Python?For example, if our dataframe is called df we just type print(df. columns) to get all the columns of the Pandas dataframe.
How do I print a column of numbers in Python?Use len() to find the number of columns. print(df). n = len(df. columns). print("Number of columns is:", n). How do I print text side by side in Python?Print two outputs side-by-side, in Python
side_by_side allows users to print two multi-line outputs side-by-side. This produces an effect similar to running diff -y file1 file2 in a Unix system.
|