从 Pandas Dataframe 打印中删除页眉和页脚
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36490263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Header and Footer from Pandas Dataframe print
提问by Dante
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
以下代码打印了我想要的所有值,但第一行是“Date”,最后一行是“Name: Close, Length: 1828, dtype: float64”
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.
如何打印没有第一个“日期”行和最后一个“名称:关闭,长度:1828,dtype:float64”行的数据?切片不起作用,我试过 print ibm["Close"][1:-1] 并且它只是切断了第 2 行和第 2 行到最后一行。
回答by Colin
print ibm["Close"].to_string(header=False)
回答by Alexander
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:
这就是 Series 对象的表示方式。您可以将其强制转换为 DataFrame,但您仍将拥有标头。但是,您可以将其设置为空字符串,然后将索引名称设置为 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:
如果要将其写入文件,则无需更改列名,只需将 header 设置为 false:
df.to_csv(filename, header=False)