如何解决类型错误:"没有要绘制的数字数据",当 Python 中存在数字数据时



我有以下数据帧,我想从中为声誉与视图创建线图,但是尽管 2 列的两种数据类型都显示它们是整数,但我得到一个错误。请建议我如何纠正它。

数据帧(原始帧具有更多行和列(

Id  Reputation  Views   UpVotes DownVotes   AccountId   ProfileImageUrl
-1  1               0   21001   27468             -1    NaN
1  21228       25360   1052    90             36500    NaN

线图代码

df_users.plot(kind='line',x='Reputation',y='Views')

错误

> TypeError                                 Traceback (most recent call
> last) <ipython-input-16-bbf2a427a767> in <module>()
> ----> 1 df_users.plot(kind='line',x='Reputation',y='Views')
> 
> 3 frames
> /usr/local/lib/python3.6/dist-packages/pandas/plotting/_matplotlib/core.py
> in _compute_plot_data(self)
>     408         # no non-numeric frames or series allowed
>     409         if is_empty:
> --> 410             raise TypeError("no numeric data to plot")
>     411 
>     412         # GH25587: cast ExtensionArray of pandas (IntegerArray, etc.) to
> 
> TypeError: no numeric data to plot

以下代码回答了我的问题:

df_users[['Views', 'Reputation']] = df_users[['Views','Reputation']].apply(pd.to_numeric) 

最新更新