1

I want to exclude the Indexcolumn from the view of a Dataframe: I sort the whole dataframe based on the values (in decending order) and assign ranks. It perfectly works, however the indexcolumn is a bit misleading (especially in the ranking). I already tried to replace the Indexcolumn and used the column Rank as an index by using

df.set_index('Rank', inplace=True)

However, the sorting is then suspended and I may get a key Error if 2 persons (like here) have the same Rank. My code is:

from scipy.stats import rankdata
import pandas as pd
from tabulate import tabulate

names = ['Tim', 'Tom', 'Sam', 'Kyle']
values = [2, 4, 5, 4]
df = pd.DataFrame({'Name': names,'Values': values})
columns = ["Name", "Values"]
df['Rank'] = df['Values'].rank(method='dense', ascending=False).astype(int)
df.sort_values(by="Rank", ascending=True)

enter image description here

Anonymosaurus
  • 99
  • 1
  • 9
  • 1
    Possible duplicate of [How to print pandas DataFrame without index](https://stackoverflow.com/questions/24644656/how-to-print-pandas-dataframe-without-index) – G. Anderson Sep 30 '19 at 16:46
  • Tried it already. It is working but the format is changing, I still need it as a table format – Anonymosaurus Sep 30 '19 at 16:52
  • You could always sort_index() if you want after set_index() But I would just consider reordering your columns and reset_index() to get a more reasonable output – zcoleman Sep 30 '19 at 16:55
  • I tried the sort_index() already, for some reason it is not working. But I will try to use the reset_index(), Thanks! – Anonymosaurus Sep 30 '19 at 16:56

1 Answers1

0

Most (possibly all?) of the pandas to_... methods take the index argument. If you set it to False the index won't be shown. If you really want the pretty HTML output in Jupyter then do

from IPython.display import HTML
HTML(df.sort_values(by="Rank", ascending=True).to_html(index=False))
tomjn
  • 5,100
  • 1
  • 9
  • 24