Pandas Print Column
In Python, the “pandas” library is utilized for analyzing, accessing, and manipulating large as well as small data. A number of Pandas modules and methods are used to perform simple to complex operations on data, such as adding, removing, extracting, displaying, and others. In Python, multiple methods are used to print the column of Pandas DataFrame.
This tutorial will provide detailed information on Pandas print column using several examples.
How to Print Pandas DataFrame Columns in Python?
To print Pandas DataFrame columns, the following methods are used in Python.
Method 1: Print Pandas DataFrame Column Using “to_string()” Function
In Python, the “to_string()” function of the “pandas” module is used to render the specified DataFrame into a console-friendly tabular output. This method can also be used to retrieve the particular column values.
Example 1: Print Pandas DataFrame Single Column
Here is an example of printing the DataFrame column:
In the above code:
- Initially, imported the “pandas” module and then created the DataFrame in the program.
- Next, the “df.to_string()” function is used twice to retrieve the DataFrame column with and without the header.
The DataFrame with and without a header has been shown in the following snippet:
Example 2: Print Pandas DataFrame Multiple Column
You can also display the multiple columns of Pandas DataFrame using provided code:
Here, the “df.to_string()” function is used to retrieve the value of the multiple column “Age” and “Height” in console tabular format.
The DataFrame multiple columns have been retrieved.
Example 3: Print Pandas DataFrame Column Without Index
The below code is used to print the Pandas DataFrame column without index value:
In the above code, the “df.to_string()” function takes the “index=False” parameter to print the specified DataFrame column.
The DataFrame column without any index has been retrieved successfully.
Method 2: Print Pandas DataFrame Column Using “Square Brackets [ ]” Method
The simple and straightforward way to print Pandas DataFrame column value is using the “Square Bracket [ ]”. This method is used in the below-stated example:
In this example code, the “df[‘Age’]” syntax is used to print values of Pandas DataFrame column “Age”.
Note: We can also pass multiple column labels to retrieve their value.
The specified DataFrame has been retrieved successfully.
Method 3: Print Pandas DataFrame Column Using “df.loc[ ]” Method
The “df.loc” method is used to access or call a group of columns or rows by utilizing their label. Let’s check out the following example to print the column of DataFrame:
Here, the “df.loc[ ]” method takes the colon “:” and the name of the column “Name” as an argument to print all the rows of the specified column.
As you can see, the specified column has been retrieved successfully:
Method 4: Print Pandas DataFrame Column Using “df.iloc[ ]” Method
The “iloc[ ]” method access or call the group of columns or rows by utilizing their index value. In the following example, this particular method is used to print the specified DataFrame column:
In this code:
- Used the “df.iloc” method that takes the index of the specified column as an argument and retrieves the value.
- Next, the index “2” of the “Height” column has been passed to the “df.iloc[ ]” method.
The values of the column have been printed.
Bonus: How to Print DataFrame Column Names in Python?
To print Pandas DataFrame column names, the “for” loop is used in Python. For example, look at the following code to get column names:
According to the above-provided code:
- First, used the “for loop” to iterate over the columns of Pandas DataFrame and displayed the column names using the “print()” function.
- Then, utilized the “df.columns” attribute to get a column of DataFrame.
The column name of the Pandas DataFrame has been returned successfully.
The “to_string()”, “Square Brackets”, “df.loc[ ]”, and “df.iloc[ ]” methods are used to print Pandas DataFrame columns in Python. These methods are used to get the single or multiple DataFrame columns values. We can also use the “for” loop to iterate over the DataFrame columns to get the names of the columns. In this write-up, we have described how to print columns of Pandas DataFrame aids examples.