pandas Python 直方图 ValueError:范围参数必须是有限的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51642846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:52:19  来源:igfitidea点击:

Python Histogram ValueError: range parameter must be finite

python-3.xpandasmatplotlib

提问by Learn

when plotting Pandas dataframe using a histogram,

使用直方图绘制 Pandas 数据框时,

sample dataframe data

样本数据帧数据

     distance
0    5.680195
2    0.000000
3    7.974658
4    2.461387
5    9.703089

code I use to plot

我用来绘制的代码

import matplotlib.pyplot as plt

plt.hist(df['distance'].values)
plt.show()

I have this error

我有这个错误

"ValueError: range parameter must be finite."  

my attempt

我的尝试

df['Round_Distance'] = df['distance'].round(1)

0    5.7
2    0.0
3    8.0
4    2.5
5    9.7

plot again, new error

再次绘图,新错误

plt.hist(df['Round_Distance'].values)
plt.show()

ValueError: max must be larger than min in range parameter.

weird thing is, the work around i use is below, i don't have to ROUND

奇怪的是,我使用的解决方法如下,我不必 ROUND

df['distance'].hist(bins=[0,25,50,75,100,125,150,175], color='g')

回答by sacuL

Sounds like you have some NaNsor infin your actual data. You can select only those values that are finite like this:

听起来您有一些NaNsinf在您的实际数据中。您只能选择像这样有限的值:

import numpy as np

df[np.isfinite(df['distance'])]

So your plot can be obtained like:

所以你的情节可以像这样获得:

plt.hist(df[np.isfinite(df['distance'])].values)

回答by Juan C

Just to add to sacul's answer, you can check if you have NaNsor infon any of your columns using this:

只是为了添加 sacul 的答案,您可以使用以下命令检查您是否拥有NaNsinf在任何列上:

For NaNs:

对于NaNs

df.isnull().sum()

For inf:

对于inf

df.max()

Hope it helps !

希望能帮助到你 !

回答by Learn

NaN cause the issue, i don't need to round it, just drop the NaN, then it works

NaN 导致问题,我不需要舍入它,只需删除 NaN,然后​​它就可以工作了

plt.hist(df['distance'].dropna().values)
plt.show()