Remove Header and Footer from Pandas Dataframe print

江枫思渺然 提交于 2021-02-07 06:47:05

问题


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.


回答1:


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



回答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)


来源:https://stackoverflow.com/questions/36490263/remove-header-and-footer-from-pandas-dataframe-print

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!