简体   繁体   中英

Remove Header and Footer from Pandas Dataframe print

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM