【问题标题】:Pandas histogram ignoring invalid data; limit x-range忽略无效数据的 Pandas 直方图;限制 x 范围
【发布时间】:2019-05-06 23:39:07
【问题描述】:

我有一个数据框,其中包含文本和数字数据的混合,-999 的一些值表示丢失或无效数据。作为一个玩具示例,假设它看起来像这样:

import pandas as pd
import matplotlib.pyplot as plt

dictOne = {'Name':['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth'],
           "A":[1, 2, -3, 4, 5, -999, 7, -999, 9],
           "B":[4, 5, 6, 5, 3, -999, 2, 9, 5],
           "C":[7, -999, 10, 5, 8, 6, 8, 2, 4]}
df2 = pd.DataFrame(dictOne)

df2.hist('C', bins = 1000)
plt.xlim=([0, 10])

这给了

我正在尝试排除 -999 值。 Pandas 中是否有一种简单的方法可以做到这一点?

另外,在我的示例代码中,为什么 x 轴不限于 [0,10] 范围?

【问题讨论】:

    标签: python pandas histogram


    【解决方案1】:

    您可以指定bins=1000,而不是指定

    df2.hist('C', bins=range(0,10))
    

    或者如果你想在中间对齐直方图框:

    df2.hist('C', bins=np.arange(0.5,11,1))
    

    输出:

    【讨论】:

      【解决方案2】:

      df2[df2['C'] > -999].hist('C') 足以满足您的所有目的。不需要指定 1000 个 bin。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-07
        • 1970-01-01
        • 1970-01-01
        • 2019-04-11
        • 2020-03-08
        • 1970-01-01
        • 2020-08-22
        相关资源
        最近更新 更多