Cod*_*bie 8 pandas pandas-groupby
我正在浏览 pandas groupby 文档,当我在特定列上分组时,如下所示:
df:
A B C D
0 foo one -0.987674 0.039616
1 bar one -0.653247 -1.022529
2 foo two 0.404201 1.308777
3 bar three 1.620780 0.574377
4 foo two 1.661942 0.579888
5 bar two 0.747878 0.463052
6 foo one 0.070278 0.202564
7 foo three 0.779684 -0.547192
grouped=df.groupby('A')
grouped.describe(A)
Run Code Online (Sandbox Code Playgroud)
给
C ... D
count mean std ... 50% 75% max
A B ...
bar one 1.0 0.224944 NaN ... 1.107509 1.107509 1.107509
three 1.0 0.704943 NaN ... 1.833098 1.833098 1.833098
two 1.0 -0.091613 NaN ... -0.549254 -0.549254 -0.549254
foo one 2.0 0.282298 1.554401 ... -0.334058 0.046640 0.427338
three 1.0 1.688601 NaN ... -1.457338 -1.457338 -1.457338
two 2.0 1.206690 0.917140 ... -0.096405 0.039241 0.174888
Run Code Online (Sandbox Code Playgroud)
描述时 25%,50%,75% 是什么意思?请解释一下?
Bab*_*pha 11
简单来说...
您将看到百分位数(25%、50%、75%..等)和它们前面的一些值。
意义在于告诉你你的数据的分布情况。
例如:
s = pd.Series([1, 2, 3, 1])
s.describe() will give
count 4.000000
mean 1.750000
std 0.957427
min 1.000000
25% 1.000000
50% 1.500000
75% 2.250000
max 3.000000
Run Code Online (Sandbox Code Playgroud)
25% 表示 25% 的数据的值为 1.0000 或以下。也就是说,如果你手动查看你的数据,其中 25% 小于或等于 1。(如果你查看我们的数据 [1, 2, 3, 1],你就会同意这一点。[1] 是25%的数据小于或等于1。
50% 表示 50% 的数据的值为 1.5 或更低。[1, 1] 占50%的数据小于或等于1.5。
75% 表示 75% 的数据的值为 2.25 或更低。[1,2,1]占75%的数据小于或等于2.25。
小智 5
要解释最小值、25%、50%、75% 和最大值,想象一下从最低值到最高值对每一列进行排序。第一个(最小)值是最小值。如果您浏览该列表的四分之一,您会发现一个大于值的 25% 且小于值的 75% 的数字。那是 25% 的值(发音为“第 25 个百分位数”)。第 50 个和第 75 个百分位数的定义类似,最大值是最大的数字。
你可以测试DataFrameGroupBy.describe:
\n\n笔记:
\n对于数字数据,结果\xe2\x80\x99s 索引将包括计数、平均值、标准差、最小值、最大值以及下、50 和上百分位数。默认情况下,下百分位数为 25,上百分位数为 75。第 50 个百分位数与中位数相同。
\n
\n\n你能解释一下上面的 foo-one 值吗?
\n
它被称为Mulitindex:
\n\n分层/多级索引非常令人兴奋,因为它为一些相当复杂的数据分析和操作打开了大门,特别是对于处理更高维的数据。本质上,它使您能够在较低维度的数据结构(如 Series (1d) 和 DataFrame (2d))中存储和操作任意数量的维度的数据。
\n
grouped=df.groupby([\'A\', \'B\'])\ndf = grouped.describe()\n\nprint (df.index)\nMultiIndex([(\'bar\', \'one\'),\n (\'bar\', \'three\'),\n (\'bar\', \'two\'),\n (\'foo\', \'one\'),\n (\'foo\', \'three\'),\n (\'foo\', \'two\')],\n names=[\'A\', \'B\'])\n\nprint (df.columns)\nMultiIndex([(\'C\', \'count\'),\n (\'C\', \'mean\'),\n (\'C\', \'std\'),\n (\'C\', \'min\'),\n (\'C\', \'25%\'),\n (\'C\', \'50%\'),\n (\'C\', \'75%\'),\n (\'C\', \'max\'),\n (\'D\', \'count\'),\n (\'D\', \'mean\'),\n (\'D\', \'std\'),\n (\'D\', \'min\'),\n (\'D\', \'25%\'),\n (\'D\', \'50%\'),\n (\'D\', \'75%\'),\n (\'D\', \'max\')],\n )\n\nprint (df.loc[(\'foo\',\'one\'), (\'C\', \'75%\')])\n-0.19421\nRun Code Online (Sandbox Code Playgroud)\n