| by Arround The Web | No comments

Pandas Print All Columns

Pandas Library offers various methods to deal with data conveniently and efficiently. Sometimes, while working with Pandas DataFrame data, we need to print all column names, values, or types for data analysis operation. To retrieve this, different methods are employed in Python.

This article will show you how to print all the column names, values, and types of a Pandas DataFrame via the below contents:

Print all Column Names in Python

To print all column names in Python, the “columns.values” method is used in Python. Here, in this code, the DataFrame with multiple columns is created with the “pandas.DataFrame()” method. Next, the “df.columns.values” method is used to print all the column names of the given Pandas DataFrame. The column names are retrieved as a list using the “list()” method:

import pandas
df = pandas.DataFrame({'Name':["Joseph","Lily","Anna","Henry"],'Age' :[16,15,13,14],'Height':[5.2, 5.1, 6.2, 5.5],'Salary':[4000,3000,5000,2040]})
print(df, '\n')
print(list(df.columns.values))

All the column names of the Pandas DataFrame have been retrieved successfully:

We can also use the “keys()” method and the “values” attribute to print all the column names of the DataFrame of Pandas. After getting the column names, we can use the “tolist()” method to convert the array of values into a Python list:

import pandas
df = pandas.DataFrame({'Name':["Joseph","Lily","Anna","Henry"],'Age' :[16,15,13,14],'Height':[5.2, 5.1, 6.2, 5.5],'Salary':[4000,3000,5000,2040]})
print(df, '\n')
print(df.keys().values.tolist())

The Pandas DataFrame column names have been displayed successfully:

Print all Column Values in Python

After printing the column names in the previous section, now we use the “pandas.set_option()” method to print all column values in Python. Here, in the below code, we import the CSV Sample data that contains a large number of rows and columns using the “pandas.read_csv()” method:

import pandas
df = pandas.read_csv("taxis.csv")
print(df)

The Pandas DataFrame output doesn’t show all columns. Some columns have dotted lines instead:

To print all the column values, the “pandas.set_option()” method takes the “display.max_columns” parameter and several columns to be displayed as an argument:

import pandas
df = pandas.read_csv("taxis.csv")
pandas.set_option('display.max_columns', None)
print(df)

All the columns have been printed to the output console successfully:

To learn more about printing or displaying all columns in Python using Pandas, you can read the Pandas Display all Columns guide for a detailed analysis.

Print all Column Types in Python

Now, after printing all column names and values, we can print all column types in Python. To print all column types, the “df.dtypes” method is used in Python. In the below-given code, the “pandas” DataFrame is created with multiple columns. To display all the column types, the “df.dtypes” is called on the input DataFrame:

import pandas
df = pandas.DataFrame({'Name':["Joseph","Lily","Anna","Henry"],
                       'Age' :[16,15,13,14],
                       'Height':[5.2, 5.1, 6.2, 5.5],
                       'Sex':['M', 'F', 'F', 'M'],
                       'Country':['USA', 'UK', 'UK', 'USA'],
                       'Id_No':[12, 15, 16, 18],
                       'Salary':[4000,3000,5000,2040]})
print(df, '\n')
print(df.dtypes)

The below output snippet shows the types of all DataFrame columns:

Conclusion

The “df.columns.values” or “df.keys().values.tolist()”, “pandas.set_option()” and “df.dtypes” method prints all columns names, values, and types of DataFrame. The “df.columns.values” is used along with the “list()” method to retrieve the column names in the list. Similarly, to print all column values, the “pandas.set_option()” is used with the “display.max_columns” attribute in Python. This tutorial delivered a detailed guide on printing all columns in Python using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply