在 pandas 中处理直方图
Manipulate histogram in pandas
我在 pandas
中有一个大型 DataFrame。我想在绘制直方图时删除具有较低频率的某些值范围(不是单个值)。
对于下图,假设我想删除对应于 count/frequency 低于 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
像这样使用 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。然后你将不得不做一个条形图,而不是历史。
我在 pandas
中有一个大型 DataFrame。我想在绘制直方图时删除具有较低频率的某些值范围(不是单个值)。
对于下图,假设我想删除对应于 count/frequency 低于 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
像这样使用 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。然后你将不得不做一个条形图,而不是历史。