Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print pandas DataFrame without index

People also ask

How can I print a DataFrame without indexing?

Using DataFrame. to_string() to Print DataFrame without Index. You can use DataFrame. to_string(index=False) on the DataFrame object to print.

How do I read a DataFrame without an index in Python?

In order to export pandas DataFrame to CSV without index (no row indices) use param index=False and to ignore/remove header use header=False param on to_csv() method.

Can I remove the index from a Pandas DataFrame?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.


python 2.7

print df.to_string(index=False)

python 3

print(df.to_string(index=False))

The line below would hide the index column of DataFrame when you print

df.style.hide_index()

Update: tested w Python 3.7


print(df.to_csv(sep='\t', index=False))

Or possibly:

print(df.to_csv(columns=['A', 'B', 'C'], sep='\t', index=False))

To retain "pretty-print" use

from IPython.display import HTML
HTML(df.to_html(index=False))

enter image description here


If you want to pretty print the data frames, then you can use tabulate package.

import pandas as pd
import numpy as np
from tabulate import tabulate

def pprint_df(dframe):
    print tabulate(dframe, headers='keys', tablefmt='psql', showindex=False)

df = pd.DataFrame({'col1': np.random.randint(0, 100, 10), 
    'col2': np.random.randint(50, 100, 10), 
    'col3': np.random.randint(10, 10000, 10)})

pprint_df(df)

Specifically, the showindex=False, as the name says, allows you to not show index. The output would look as follows:

+--------+--------+--------+
|   col1 |   col2 |   col3 |
|--------+--------+--------|
|     15 |     76 |   5175 |
|     30 |     97 |   3331 |
|     34 |     56 |   3513 |
|     50 |     65 |    203 |
|     84 |     75 |   7559 |
|     41 |     82 |    939 |
|     78 |     59 |   4971 |
|     98 |     99 |    167 |
|     81 |     99 |   6527 |
|     17 |     94 |   4267 |
+--------+--------+--------+