python - 数据框中所有值的频率分布

标签 python pandas matplotlib

我有以下数据框,例如mydata:

             1        10       100    ...           97        98        99
0     23.53500  23.53500  23.53500    ...     23.53500  23.53500  23.53500
1     24.20000  24.20000  24.20000    ...     24.20000  24.20000  24.20000
2     23.71500  23.71500  23.71500    ...     23.71500  23.71500  23.71500
3     23.24000  23.24000  23.24000    ...     23.24000  23.24000  23.24000
4     22.79500  22.79500  22.79500    ...     22.79500  22.79500  22.79500
5     18.70150  18.70150  18.70150    ...     18.70150  18.70150  18.70150
6     10.56550  10.56550  10.56550    ...     10.56550  10.56550  10.56550
7      0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
8      0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
9      0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
10     0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
11     1.24300   1.24300   1.24300    ...      1.24300   1.24300   1.24300
12     7.68400   7.68400   7.68400    ...      7.68400   7.68400   7.68400
13     1.46900   1.46900   1.46900    ...      1.46900   1.46900   1.46900
14    10.50900  10.50900  10.50900    ...     10.50900  10.50900  10.50900
15    18.58850  18.58850  18.58850    ...     18.58850  18.58850  18.58850
16    22.93500  22.93500  22.93500    ...     22.93500  22.93500  22.93500
17    23.54500  23.54500  23.54500    ...     23.54500  23.54500  23.54500
18    24.21500  24.21500  24.21500    ...     24.21500  24.21500  24.21500
19    25.23000  25.23000  25.23000    ...     25.23000  25.23000  25.23000
20    25.88100  25.88100  25.88100    ...     25.88100  25.88100  25.88100
21    26.33775  26.33775  26.33775    ...     26.33775  26.33775  26.33775
22    26.72000  26.72000  26.72000    ...     26.72000  26.72000  26.72000
23    26.98000  26.98000  26.98000    ...     26.98000  26.98000  26.98000
24    27.19500  27.19500  27.19500    ...     27.19500  27.19500  27.19500
25    27.34500  27.34500  27.34500    ...     27.34500  27.34500  27.34500
26    27.52000  27.52000  27.52000    ...     27.52000  27.52000  27.52000
27    27.86500  27.86500  27.86500    ...     27.86500  27.86500  27.86500
28    28.41500  28.41500  28.41500    ...     28.41500  28.41500  28.41500
29    29.06750  29.06750  29.06750    ...     29.06750  29.06750  29.06750
       ...       ...       ...    ...          ...       ...       ...
6931  28.61000  28.61000  28.61000    ...     28.61000  28.61000  28.61000
6932  28.97825  28.97825  28.97825    ...     28.97825  28.97825  28.97825
6933  29.06225  29.06225  29.06225    ...     29.06225  29.06225  29.06225
6934  28.49000  28.49000  28.49000    ...     28.49000  28.49000  28.49000
6935  27.56000  27.56000  27.56000    ...     27.56000  27.56000  27.56000
6936  26.64225  26.64225  26.64225    ...     26.64225  26.64225  26.64225
6937  26.15400  26.15400  26.15400    ...     26.15400  26.15400  26.15400
6938  26.16450  26.16450  26.16450    ...     26.16450  26.16450  26.16450
6939  25.95450  25.95450  25.95450    ...     25.95450  25.95450  25.95450
6940  25.62900  25.62900  25.62900    ...     25.62900  25.62900  25.62900
6941  25.32975  25.32975  25.32975    ...     25.32975  25.32975  25.32975
6942  24.85725  24.85725  24.85725    ...     24.85725  24.85725  24.85725
6943  24.06500  24.06500  24.06500    ...     24.06500  24.06500  24.06500
6944  23.15000  23.15000  23.15000    ...     23.15000  23.15000  23.15000
6945  19.43600  19.43600  19.43600    ...     19.43600  19.43600  19.43600
6946  14.06850  14.06850  14.06850    ...     14.06850  14.06850  14.06850
6947   9.15300   9.15300   9.15300    ...      9.15300   9.15300   9.15300
6948   4.57650   4.57650   4.57650    ...      4.57650   4.57650   4.57650
6949   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6950   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6951   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6952   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6953   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6954   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6955   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6956   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6957   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6958   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6959   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000
6960   0.00000   0.00000   0.00000    ...      0.00000   0.00000   0.00000

[6961 rows x 149 columns]

我想创建所有数据的频率分布/直方图(例如6961x149)。我尝试使用 matplotlib 和 dataframe.hist() 来区分数据框中的每一列,这不是我想要的。我可以以某种方式编写一个代码,其中绘图函数不区分列和行,而只是制作数据框中所有值的直方图吗?

示例代码和图:

import matplotlib.pyplot as plt

plt.hist(mydata.values,bins=20)
plt.show()

mydata.plot.hist()

结果:

matplotlib

enter image description here

最佳答案

解决方案是使用stack()将所有数据放入一列中,然后制作直方图:

df = pd.DataFrame([[1,2,3], [3,4,5], [2,3,4], [8,5,2], [3,2,1]])
df.stack().plot.hist()

enter image description here

关于python - 数据框中所有值的频率分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52854574/

相关文章:

Python matplotlib默认在 Axis 外绘制曲线?

python - 迭代并行列表并将一个列表中的制表符分隔文本转换为其他列表中的 csv 路径

javascript - 从抓取的 Javascript 表列表创建 DataFrame

python - pandas 替换列子集的空值

python - 来自 x、y 值的 Matplotlib 直方图,日期时间月份作为 bin

python - 当数据大小大于axis时,matplotlib如何决定显示什么

python - 从时间序列中插值单个值

python - 如何在python中将2D数组组合成3D数组?

python - 获取字符串形式的参数名称 - Python

python - Pandas dataframe 使用 pd.concat 将字符串替换为 NaN