In Python, the “Pandas” library is employed for data manipulation and data analysis tasks/operations. Sometimes while working in Pandas for larger datasets, the output displayed, such as columns or rows, is truncated and not shown completely. To retrieve all the columns in Python Pandas, the “pandas.set_option()” is used in Python.
This blog provides a comprehensive guide on displaying all columns using the below contents:
- How to Display All Columns in Python?
- Display All Columns in Python Using “pandas.set_option()”
- Display Specific Columns in Python Using “pandas.set_option()”
- Display Columns with Specified Width in Python Using “pandas.set_option()”
- Display All Columns Names in Python Using “df.columns.tolist()”
- Display All Rows in Python Using “pandas.set_option()”
How to Display All Columns in Python?
To display all columns in Python, the “pandas.set_option()” function is used with the specified option “display.max_columns” in Python. The “pandas.set_option()” is used to set various options and configurations while dealing with Pandas. We can use several options, such as “display.max_columns”, “display.max_colwidth”, and “display.max_rows”. Other options to control the behavior and displays of the Pandas object such as DataFrame and Series.
Syntax
Parameters
In the above syntax, the “param” and the “value” parameters indicate the available options and values to control the Pandas object.
Return Value
This function does not retrieve any value by default, but the “OptionError” is retrieved when no such option is passed to the function.
Example 1: Display All Columns in Python Using pandas.set_option()
The pandas.set_option() is used in Python to display/show all Pandas DataFrame columns. For example, let’s consider an example CSV Sample Data that is used to display in Python. Here is an example code that reads and displays the CSV data using the “pandas.read_csv()” method:
df = pandas.read_csv("taxis.csv")
print(df)
The above code displays some of the columns and rows of Pandas DataFrame:
To display all the columns in Pandas DataFrame, the “pandas.set_option()” takes the “display.max_columns” along with the maximum column value as an argument.
df = pandas.read_csv("taxis.csv")
pandas.set_option('display.max_columns', None)
print(df)
The above code executed and displays all the columns of the given CSV file:
Output Continues
Note: To get back the original result, we need to reset the max column output using the “pandas.reset_option(‘max_columns’)” function.
Example 2: Display Specific Columns in Python Using pandas.set_option()
To display the specified columns, the “display.max_columns” option and the number of columns we need to display are passed to the “pandas.set_option()” function. Here is an example code:
df = pandas.read_csv("taxis.csv")
pandas.set_option('display.max_columns', 9)
print(df.head(2))
The above code displays the specified column value to the output:
Example 3: Display Columns with Specified Width in Python Using pandas.set_option()
The “pandas.set_option()” function can display columns with specified widths. For example, in the below code, we read the CSV that contains specified column text data that was originally truncated from the end of the text:
df = pandas.read_csv("new.csv")
print(df)
The execution of the above code retrieved the below output:
Now, to modify the width of the specified column, the “display.max_colwidth” option, along with the width size “150”, is passed to the “pandas.set_option()” method:
df = pandas.read_csv("new.csv")
pandas.set_option('display.max_colwidth', 150)
print(df['Text'])
The width size of the specified column has been changed successfully:
Example 4: Display All Columns Names in Python Using df.columns.tolist()
We can also display all the column names of the specified DataFrame without their values as a list. In the below code, the “df.columns.tolist()” method is used to retrieve all the column’s names in the form of a list:
df = pandas.read_csv("taxis.csv")
print(df.columns.tolist())
All the DataFrame column names in Python have been displayed:
Example 5: Display All Rows in Python Using pandas.set_option()
The “pandas.set_option()” method can also utilize the “display.max_rows” option to display all rows in Python. Take the following code to achieve this:
df = pandas.read_csv("taxis.csv")
pandas.set_option('display.max_rows', None)
print(df)
All rows of the DataFrame have been displayed successfully:
Output Continues
Conclusion
To display all the columns in Python the “pandas.set_option()” function takes the “display.max_columns” option and its value as an argument. We can also use other options, such as “display.max_colwidth” and the “display.max_rows” option to modify the column width and display all rows. The “df.columns.tolist()” method is used to display all column names in the list. This article delivered a detailed guide on displaying all columns in Python using various examples.