Explain Codes LogoExplain Codes Logo

How to print pandas DataFrame without index

python
dataframe
pandas
data-visualization
Alex KataevbyAlex Kataev·Jan 3, 2025
TLDR

To print a DataFrame without the index, swiftly apply the Panda's function to_string(index=False):

print(df.to_string(index=False))

What was:

 A  B
 1  4
 2  5
 3  6

Now index-less! A tidy table, just the data. Let's go further down this rabbit hole.

Beyond basic printing

Neatly hide index using style

Conjure a stylish dataframe in your Jupyter notebook with style.hide_index():

print(df.style.hide_index()) # A catwalk model, without barcodes!

Now you have a clean slate to work with, impressing your colleagues and terrifying your enemies.

Time travel with datetime formatting

When dealing with time travelers (or the timestamp), extract and format time:

df['time'] = df['datetime'].dt.strftime('%H:%M:%S') # Turns datetime into a Rolex watch

It's like your DataFrame took its sunglasses off and looked directly at the clock.

Exporting without index

Do you need the data elsewhere minus the index? One index=False coming right up:

df.to_csv('file.csv', index=False, sep=';') # Less baggage, more legroom!

With sep set, DataFrame breaks the language barrier and speaks CSV fluently.

Fancy console prints with tabulate

Welcome to the console-print renaissance. Dress up your DataFrame with tabulate:

from tabulate import tabulate print(tabulate(df, headers ='keys', tablefmt='psql', showindex=False)) # The little black dress for data

tablefmt accessorizes the style, showindex=False keeps things minimalistic.

Slicing and dicing data

Carve up the DataFrame, like a master sushi chef:

subset = df.loc[:, ['A', 'B']] # .loc targets columns by names. A. B. C. Easy as... subset = df.iloc[:, 0:2] # .iloc for indices. Made for number-enthusiasts.

Fancy HTML display

To paint an HTML-worthy picture:

from IPython.display import HTML HTML(df.to_html(index=False)) # When .to_string seems too mainstream

This puts the DataFrame in an HTML tuxedo; it's Oscar-ready now.

Visual aid

Let's make it visual with everyone's favorite – emojis. Here's what we've achieved:

DataFrame | A | B | | ------| ---| | 1 | 4 | | 2 | 5 | | 3 | 6 |

Turns into:

DataFrame A B 1 4 2 5 3 6

With to_string(index=False), Index checks out from Airbnb(DataFrame).