数据分析 常见异常及解决办法(一)

时间:2022-07-24
本文章向大家介绍数据分析 常见异常及解决办法(一),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.Jupyter读取数据警告ParserWarning: Falling back to the ‘python’ engine because the ‘c’ engine does not support regex separators

在使用Jupyter Notebook读取数据进行分析时,如下:

<ipython-input-5-9af9eaa72e92>:5: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from 's+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
  users = pd.read_csv('users.dat',sep = '::',header = None,names = labels)

提示C引擎不支持正则表达式分割,需要使用Python引擎,此时只需要在读取数据文件时加入参数,engine='python'即可,如下:

users = pd.read_csv('users.dat',sep = '::',header = None,names = labels, engine='python')

此时再执行就不会再提示警告信息了。

2.使用matplotlib画图警告 RuntimeWarning: Glyph 30005 missing from current font

在使用matplotlib库进行画图时,如果标题等文字中出现中文,就可能出现警告:

E:Anaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:214: RuntimeWarning: Glyph 30005 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:Anaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:214: RuntimeWarning: Glyph 24433 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:Anaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:214: RuntimeWarning: Glyph 25968 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:Anaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:214: RuntimeWarning: Glyph 25454 missing from current font.
  font.set_text(s, 0.0, flags=flags)
E:Anaconda3libsite-packagesmatplotlibbackendsbackend_agg.py:214: RuntimeWarning: Glyph 25454 missing from current font.
  font.set_text(s, 0.0, flags=flags)

意思是plt画图找不到字体,需要进行配置。 有两种方式:

  • 临时设置 在调用画图函数前进行配置: plt.rcParams['font.sans-serif']=['Microsoft YaHei'] #显示中文标签 plt.rcParams['axes.unicode_minus']=False 即设置字体为微软雅黑,支持中文。 但是这只是临时设置,下一次再使用又得设置,显得很麻烦。
  • 永久配置 永久设置是编辑matplotlib的配置文件matplotlibrc,修改后以后无需再修改、一劳永逸。该文件一般位于%PythonPath%Libsite-packagesmatplotlibmpl-data(PythonPath即表示安装的Python路径),如果使用的是Anaconda,则是%AnacondaPath%Libsite-packagesmatplotlibmpl-data(AnacondaPath表示Anaconda的安装路径)。 在matplotlibrc文件中找到如下位置(定义font.family处):

修改如下: ## The font.size property is the default font size for text, given in pts. ## 10 pt is the standard value. ## ## Note that font.size controls default text sizes. To configure ## special text sizes tick labels, axes, labels, title, etc, see the rc ## settings for axes and ticks. Special text sizes can be defined ## relative to font.size, using the following values: xx-small, x-small, ## small, medium, large, x-large, xx-large, larger, or smaller font.family : monospace font.monospace : Microsoft YaHei, SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif #font.style : normal #font.variant : normal #font.weight : normal #font.stretch : normal #font.size : 10.0 主要是第10、11行,设置font.family : monospace取消#注释,添加一行font.monospace : Microsoft YaHei, SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serifMicrosoft YaHei为微软雅黑、SimHei为黑体、支持中文。 然后再重启Jupyter Notebook或者重新运行代码即可显示中文,如下: