Python

Pandas Display All Columns

Sometimes, the user needs to see only the column labels in the given DataFrame. Pandas have very useful functions like head(), tail(), keys(), info(), etc. to get all the column details. Let’s discuss the different scenarios to get all the column names using the existing methods.

First, we create a DataFrame with 5 columns and we use this DataFrame in all table styles.

Note: Make sure to run this code in your environment because we use this code in all our examples. Otherwise, you will get errors.

import pandas

# Consider the DataFrame having 10 records with 5 columns
management=pandas.DataFrame({'manager id':[1,2,3,4,5,6,7,8,9,10],
                          'name':['pill','dee dee','ghorak','teon','marky','pill','dee dee','ghorak','teon','marky'],
                        'Idea':['House drainage','All','water supply','electricity','drilling','All','water supply','electricity','electricity','irrigation'],
                        'demography':['ap','gujarat','patna','indore','norway','ap','gujarat','patna','indore','norway']})


print("Actual: \n")
print(management)

Output:

Scenario 1: Display the Column Names Using Head() and Tail()

We use the head() and tail() methods to display the top and last rows in the DataFrame. In this, we also get the column names along with the rows. The head() displays the first 5 rows by default and the tail() returns the last 5 rows by default.

Syntax:
First –

DataFrame_object.head(n)

Last –

DataFrame_object.tail(n)

Example 1: Display the Top Rows
Let’s display the first 3 and 7 records separately from the DataFrame which we created at the beginning of this article.

# Display top 3 records
print(management.head(3))

print()

# Display top 7 records
print(management.head(7))

Output:

In both results, we can see that the column names are returned along with the rows.

Example 2: Display the Last Rows
Let’s display the last 5 rows.

# Display last 5 records
print(management.tail())

Output:

We can see that the column names are returned along with the rows.

Scenario 2: Using the Columns

Columns are the attribute in Pandas DataFrame which returns all the column names in an indexed object.

Syntax:

DataFrame_object.columns

Example 1:

# Get all columns using columns
print(management.columns)

Output:

The column names are returned in an index object.

Example 2: Columns Using the For Loop
In the previous example, the column names are returned in an index object. If you only want the columns, you can iterate the index object using the for loop and display the column names.

# Get all columns from the management DataFrame
for all in management.columns:
    print(all)

Output:

Now, you can see that only the column names are returned.

Example 3: Columns Using Values
In the previous example, the column names are returned in an index object. If you only want the columns in a list, you can use the values method along with the columns.

# Get all columns using columns.values in a list
print(management.columns.values)

Output:

Now, you can see that only the column names are returned in a list.

Scenario 3: Using Keys()

Keys() is similar to columns which returns the column names in an indexed object. We can use the values along with keys() to return only the column names in a list.

Syntax:

DataFrame_object.keys()
DataFrame_object.keys().values

Example:

# Get columns using keys()
print(management.keys())

# Using keys() with values
print(management.keys().values)

Output:

In the first output, the columns are returned in an indexed object. But in the second output, the columns are returned in a list.

Scenario 4: Using Info()

If you want to display the Data Type along with the column and Non-Null count, you can use the info() method. It also returns the RangeIndex. The memory is used by the DataFrame.

Syntax:

DataFrame_object.info()

Example:

# Get columns along with data types and non-null values count.
print(management.info())

Output:

Scenario 5: Return Only the Numeric Columns

If you want to return the columns of the numeric type like int64, float, etc., you can use the _get_numeric_data() method. It returns the columns in an indexed object.

Syntax:

DataFrame_object._get_numeric_data().columns

Example:
Let’s return only the numeric column names.

# Get only numeric columns
print(management._get_numeric_data().columns)

Output:

DataFrame_object.describe() is another method which returns the statistics of numeric columns. We can utilize this method so that we can get the column name.

You can put the columns after describe(), to see only the column names in an indexed object.

Example:
Let’s return only the numeric column names using the describe() method.

# Get only numeric columns to display the statistics
print(management.describe())
print(management.describe().columns)

Output:

In the first output, you can see that only the “manager id” is the column that displays the statistics. It is the numeric column. In the second output, we return the indexed object that holds the column name.

Scenario 6: Return the Columns Based on Condition

We can return the column names based on the data type using the condition.

Syntax:

DataFrame_object.dtypes[condition].index.values

Example:
Let’s return the columns which are of type “int64” and “object”, separately.

# Get the columns of type int64
print(management.dtypes[management.dtypes == "int64"].index.values)

# Get the columns of type object
print(management.dtypes[management.dtypes == "object"].index.values)

Output:

In the first output, you can see that only the “manager id” is the column with type “int64”. In the second output, there are three columns of type “object”.

Conclusion

We saw six different scenarios to display all column names. We also utilized the values and columns mostly in all the scenarios, as they return the column names in a concise way. By the end of this article, you will know how to display the column names and how to filter the columns using the data types. Also, you can view the other details like DataFrame memory usage with the info() method and summary statistics with describe().

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain