pandas 如何在单行上打印 DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39482722/
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
How to print DataFrame on single line
提问by alphanumeric
With:
和:
import pandas as pd
df = pd.read_csv('pima-data.csv')
print df.head(2)
the print is automatically formatted across multiple lines:
打印件自动跨多行格式化:
num_preg glucose_conc diastolic_bp thickness insulin bmi diab_pred \
0 6 148 72 35 0 33.6 0.627
1 1 85 66 29 0 26.6 0.351
age skin diabetes
0 50 1.3790 True
1 31 1.1426 False
I wonder if there is a way to avoid the multi-line formatting. I would rather have it printed in a single line like so:
我想知道是否有办法避免多行格式。我宁愿将它打印在一行中,如下所示:
num_preg glucose_conc diastolic_bp thickness insulin bmi diab_pred age skin diabetes
0 6 148 72 35 0 33.6 0.627 50 1.3790 True
1 1 85 66 29 0 26.6 0.351 31 1.1426 False
回答by jezrael
You need set:
你需要设置:
pd.set_option('expand_frame_repr', False)
option_context
context manager has been exposed through the top-level API, allowing you to execute code with given option values. Option values are restored automatically when you exit the with block:
option_context
上下文管理器已通过顶级 API 公开,允许您使用给定的选项值执行代码。退出 with 块时,选项值会自动恢复:
#temporaly set expand_frame_repr
with pd.option_context('expand_frame_repr', False):
print (df)