Remove Header and Footer from Pandas Dataframe print

21,584

Solution 1

print ibm["Close"].to_string(header=False)

Solution 2

That is how a Series object is represented. You can coerce it to a DataFrame, but you will still have the header. You can set that to be an empty string, however, and then set the index name to None:

df = ibm[['Close']]
df.columns = ['']
df.index.name = None
>>> print(df)
2009-01-02   87.370003
2009-01-05   86.820000
               ...
2016-04-06  150.020004
2016-04-07  148.250000

[1828 rows x 1 columns]

If you are going to write it to a file, you don't need to change the column name, just set header to false:

df.to_csv(filename, header=False)
Share:
21,584
Dante
Author by

Dante

Updated on August 02, 2022

Comments

  • Dante
    Dante almost 2 years

    The following code prints all the values I want but has "Date" as the first row and "Name: Close, Length: 1828, dtype: float64" as the last row

    import pandas as pd
    from pandas.io.data import DataReader
    from datetime import datetime
    
    ibm = DataReader('IBM',  'yahoo', datetime(2009,1,1))
    pd.set_option('display.max_rows',len(ibm))
    print ibm["Close"]
    

    How do I print the data w/o this first "Date" line and the last "Name: Close, Length: 1828, dtype:float64" line? Slicing doesn't work, I've tried print ibm["Close"][1:-1] and it just cuts off the 2nd and 2nd to last row.

  • Dante
    Dante about 8 years
    Traceback (most recent call last): File "scrapetest.py", line 7, in <module> print ibm["Close"].to_string(header=False) TypeError: to_string() got an unexpected keyword argument 'Header'
  • Colin
    Colin about 8 years
    Which version of pandas are you running?
  • ikel
    ikel about 7 years
    what if just show dataframe in python notebook? dont want to write it to disk
  • Alexander
    Alexander about 7 years
    df.rename(columns={col: "" for col in df})