【问题标题】:Manipulate histogram in pandas在 pandas 中操作直方图
【发布时间】:2019-10-17 21:12:34
【问题描述】:

我在pandas 中有一个大数据框。我想在绘制直方图时删除具有较低频率的某些值范围(不是单个值)。

对于下面的图片,假设我要删除对应于计数/频率低于 20 的 Dataframe 变量的所有值。有人对此有任何解决方案吗?

# PR has value between 0 to 1700 
data['PR'].hist(bins = 160) #image on the left
data_openforest['PR'].hist(bins = 160) #image on the right

【问题讨论】:

  • 您可以使用np.histogrampd.cut 来计算直方图并过滤计数。
  • 你有一个例子吗?我到了ranges = [i for i in np.arange(0,1600,10)]count = data_openforest.groupby(pd.cut(data_openforest['Count'], ranges)).count()。但是我现在如何将它应用到我的原始数据框。

标签: python pandas histogram


【解决方案1】:

像这样使用 pd.cut 应该可以:

out = pd.cut(data_openforest['PR'], bins=160)
counts = out.value_counts(sort=False)
counts[counts > 20].plot.bar()
plt.show()

如果你想过滤你的 DataFrame,你必须这样做:

data_openforest['bin'] = pd.cut(data_openforest['PR'], bins=160)
bin_freq = data_openforest.groupby('bin').count()
data_openforest = data_openforest.merge(bin_freq, 
                                        on='bin', 
                                        how='left',
                                        suffixes=("_bin", 
                                                  "_bin_freq"))

然后您可以轻松过滤您的 DataFrame。然后你将不得不做一个条形图,而不是一个历史。

【讨论】:

  • 是的,我的代码也知道了范围。但我必须手动查看并进行过滤。你能告诉任何使用你直接提到的代码来过滤数据框而无需干预的方法吗?
  • 已编辑。不确定您所说的“无需干预”是什么意思。
  • 是的。谢谢,这正是你刚刚所做的。我不确定如何做最后一步。
猜你喜欢
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
相关资源
最近更新 更多