我在pandas
中有一个大型DataFrame。我想在绘制直方图时删除具有较低频率的某些值范围(不是单个值)。
对于下面的图片,假设我要删除Dataframe变量的所有值,这些值对应于20以下的计数/频率。有人对此有任何解决方案吗?
# 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
答案 0 :(得分: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。然后,您将不得不绘制条形图,而不是历史。