我正在使用pandas DataFrame。如果打印dataframe,它包含一些用脚本(-)代替数字的列。当我使用数据制作图表时,termninal返回:
TypeError: Empty 'Series': no numeric data to plot
这些脚本(-)数据点不能在制作图形时使用吗?它们需要更换吗?如果是这样,怎么办?在
我写的代码是:
^{pr2}$示例数据
data.csv
a,b,c,d,e,f,g,h,i,j,k,l,m,n,nn,o,p,q,r,s,t,u,v,w,x,y,z,hh
12,2798,3.9,3.0,1.1,4.0,0.1,5.0,0.0,-,0.1,35.5,0.2,52.0,1.6,19.0,2.0,36.0,0.1,24.5,0.2,52.0,0.2,2.0,0.0,-,0.2,13.0
13,2757,8.5,6.0,3.4,15.0,0.1,1.0,0.0,3.0,0.0,-,0.6,6.0,2.5,12.0,2.4,14.0,0.1,41.0,0.3,25.5,0.3,4.0,0.1,5.0,0.1,19.0
14,2792,18.1,5.0,8.7,24.0,0.1,5.5,0.0,-,0.1,2.0,1.6,3.0,2.6,5.0,3.9,12.0,0.4,4.5,0.9,5.0,0.8,12.0,0.1,24.0,0.2,16.5
15,2956,29.2,6.0,14.5,25.0,0.5,4.0,0.1,9.5,0.2,1.0,2.1,4.0,2.5,5.5,5.5,10.0,0.8,3.0,2.0,4.5,1.5,6.0,0.3,10.5,0.4,30.0
使用埃文的答案:df.replace(to_replace = '-', value = np.nan, inplace = True)
仍然得到相同的错误。在
它适用于第一列,因为这些列没有脚本(-)
好吧,根据你的意见,你有两个选择:
-
符号,则删除整行。在-
符号,则为其设置默认值。在假设我有以下数据帧:
选项1
^{pr2}$选项2
借用@Scratch'N'Purr的代码,我的解决方案如下:
数据框:
^{pr2}$输出:
情节:
{a1}
我没有仔细查看数据;这个解决方案是否符合您的期望?第二,从}在计算上很昂贵,但是对于足够小的数据集来说足够好。在
int
到{编辑:参见pandas DataFrame "no numeric data to plot" error
为了绘制
NaN
,请转换为float
。以上(编辑的)代码生成了PNG图像a到hh,基于OP的更新帖子。在相关问题 更多 >
编程相关推荐