pandas print out all columns

Pandas Print All Columns

By Ashley Allen
AD

Ever found yourself poring over a DataFrame in pandas, only to wish you could print out all its columns at once? While pandas doesn't provide a built-in function for this, we can certainly achieve it through a few simple lines of code and a bit of understanding about pandas' data structures. Let's dive in and see how.

Before we start, let's first understand why you might need to do this. You might be exploring a new dataset, wanting to know its structure, or you might need to лет your data for analysis or visualization. Whatever your reason, knowing how to print all columns can be a timesaver and a useful tool in your pandas arsenal.

Using head() and tail() Functions

The head() and tail() functions are pandas' out-of-the-box offering for printing data. But, by default, they only show up to 5 rows, and no columns. To print all columns, we need to tweak them a bit.

AD
panda bear lined notepads with blank paper for notes or reminders, set of 4
panda bear lined notepads with blank paper for notes or reminders, set of 4
Free Printable Panda Worksheets (w/ Life Cycle and Coloring Pages)
Free Printable Panda Worksheets (w/ Life Cycle and Coloring Pages)
several pandas are shown in black and white on a gray background with the same pattern
several pandas are shown in black and white on a gray background with the same pattern

First, let's use head(). By passing a parameter, we can change the number of rows to print. But there's a catch - it won't show all columns. To get around this, we can use the T attribute for transposition:

Transposing DataFrame with T

Transposing your DataFrame with the T attribute allows you to print all columns. This works because transposition switches rows and columns:

panda face templates for kids to print out and use on the back of their head
panda face templates for kids to print out and use on the back of their head
two pandas are sitting on bamboo stalks and one is looking up at the sky
two pandas are sitting on bamboo stalks and one is looking up at the sky
an image of pandas and flowers on the app store's wallpaper page
an image of pandas and flowers on the app store's wallpaper page
```python df.head(10).T ```

This will print the first 10 rows and all columns. If you want to print all rows, you can simply do:

```python df.T ```

Now let's move on to the tail() function, which shows the last n rows of the DataFrame. You can use the same trick here:

AD

Using tail() with Transposition

```python df.tail(10).T ```

Or to print all rows:

Panda
Panda
Панда
Панда
four panda stickers sitting on top of each other in front of some bamboo leaves
four panda stickers sitting on top of each other in front of some bamboo leaves
two panda bears sitting in front of a bamboo frame
two panda bears sitting in front of a bamboo frame
Panda Cartoon
Panda Cartoon
the pandas are all lined up on the wall
the pandas are all lined up on the wall
```python df.tail().T ```

Using Stylesheet Function

If you're comfortable with CSS, consider using the style function in combination with a simple html table. This allows you to format the output:

Table Formatting with style

```python df.style.hide_index().render() ```

This will print all columns in a pretty, formatted table, starting from the first row. If you need to start from a specific row, you can use the loc function:

AD
```python df.style.hide_index().render("loc", row_start=0, row_end=None) ```

Finally, Column Width

By default, the resulting table won't fit all your columns on one line. If your columns are wide and you don't want to scroll horizontally, you can adjust the column widths:

a panda bear sitting on top of a bamboo tree holding onto a green branch with leaves
a panda bear sitting on top of a bamboo tree holding onto a green branch with leaves
```python df.style.set_properties(subset=pd.np.arange(len(df.columns)), width='100%').hide_index() ```

This adjusts every column to take up 100% of the width.

In your pandas journey, it's likely that you'll find yourself wishing you could see all columns at once. With these methods, you have a variety of ways to do just that, whether you're exploring data, checking for anomalies, or preparing for analysis.

AD
AD