How to Print Pandas Dataframe Without Index

How to print pandas DataFrame without index

python 2.7

print df.to_string(index=False)

python 3

print(df.to_string(index=False))

How can I print out just the index of a pandas dataframe?

You can access the index attribute of a df using .index:

In [277]:

df = pd.DataFrame({'a':np.arange(10), 'b':np.random.randn(10)})
df
Out[277]:
a b
0 0 0.293422
1 1 -1.631018
2 2 0.065344
3 3 -0.417926
4 4 1.925325
5 5 0.167545
6 6 -0.988941
7 7 -0.277446
8 8 1.426912
9 9 -0.114189
In [278]:

df.index
Out[278]:
Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')

How to print a Pandas dataframe to Latex without the index?

You want index=False, not index_names=False:

>>> df.to_latex(index=False)
'\\begin{tabular}{rr}\n\\toprule\n A & B \\\\\n\\midrule\n 1 & 3 \\\\\n 2 & 4 \\\\\n\\bottomrule\n\\end{tabular}\n'

What index_names=False does is remove the line beneath the column headers that contains the name of the index levels. This only happens when there is an index name. See the lines with idx below:

>>> df.rename_axis('idx')
A B
idx
0 1 3
1 2 4
>>> df.rename_axis('idx').to_latex()
'\\begin{tabular}{lrr}\n\\toprule\n{} & A & B \\\\\nidx & & \\\\\n\\midrule\n0 & 1 & 3 \\\\\n1 & 2 & 4 \\\\\n\\bottomrule\n\\end{tabular}\n'
>>> df.rename_axis('idx').to_latex(index_names=False)
'\\begin{tabular}{lrr}\n\\toprule\n{} & A & B \\\\\n\\midrule\n0 & 1 & 3 \\\\\n1 & 2 & 4 \\\\\n\\bottomrule\n\\end{tabular}\n'

How can I get only one column of Pandas dataframe (without index) and put it into deque?

Use Series.tolist to convert the columns' values to lists

data = [[ticker, df[ticker].tolist()] for ticker in tickers]
c = deque(data)


Related Topics



Leave a reply



Submit