为何Python代码中tsla_df.describe(include="all")无输出且无报错?
问题:为什么
tsla_df.describe(include="all")既无输出也无报错? 先看你提供的完整代码:
from abupy import ABuSymbolPd import matplotlib.pyplot as plt import numpy as np import pandas as pd tsla_df = ABuSymbolPd.make_kl_df('usTSLA', n_folds=8) tsla_df [['close', 'volume']].plot (subplots = True, style = ['r', 'g'], grid = True) print tsla_df [ ['close', 'volume']] plt.show() tsla_df.info() tsla_df.describe(include = "all")
核心原因
在普通Python脚本环境中,pandas.DataFrame.describe()方法只会返回一个包含统计信息的DataFrame对象,但不会主动将其打印出来。你之前的print tsla_df[...]用了print语句才触发了输出,而最后一行的describe()只是生成了结果却没有执行输出动作,所以看不到任何内容;同时因为代码本身没有语法或逻辑错误,自然也不会报错。
另外补充一个小细节:如果你的环境是Python3,print tsla_df [...]这行其实会触发语法错误(Python3要求print是带括号的函数),你说没有报错,推测你可能在用Python2环境,但这和describe()无输出的问题无关。
解决办法
有两种简单的方式可以让统计信息显示出来:
- 用
print()包裹describe()调用:
(Python2里print(tsla_df.describe(include = "all"))print可以不带括号,但推荐加上,这样能兼容Python3环境) - 如果是在Jupyter Notebook/Lab环境:
把tsla_df.describe(include = "all")单独放在代码单元格的最后一行,Jupyter会自动显示该语句的返回结果,不需要额外的print。
内容的提问来源于stack exchange,提问作者Alivin Wang
相关产品推荐
相关产品推荐

