if df.isnull().all().all():
print("DataFrame contains only NaN values")
3. 在绘图前的使用场景
在绘图前,建议先检查 DataFrame 是否为空,避免报错或生成空图:
python
import matplotlib.pyplot as plt
if df.empty:
print("DataFrame is empty, cannot plot.")
else:
df.plot()
plt.show()
总结
方法
适用场景
df.empty
判断 DataFrame 是否完全为空(无行或列)
len(df) == 0
检查行数是否为 0
df.isnull().all()
检查所有值是否为 NaN
根据具体需求选择合适的方法即可。
User
IndentationError: unindent does not match any outer indentation level,这是什么意思,怎么解决
Assistant
在 Python 中,IndentationError: unindent does not match any outer indentation level 是一个常见的语法错误,表示 代码块的缩进层级不匹配。Python 通过缩进来区分代码块(如函数、循环、条件语句等),如果缩进不一致,就会触发此错误。