python - 按两列分组并将不同的分位数打印为单独的列

标签 python python-3.x pandas pandas-groupby

这是一个可重现的示例:

import pandas as pd

df = pd.DataFrame([['Type A', 'Event1', 1, 2, 3], ['Type A', 'Event1', 4, 5, 6], ['Type A', 'Event1', 7, 8, 9],
['Type A', 'Event2', 10, 11, 12], ['Type A', 'Event2', 13, 14, 15], ['Type A', 'Event2', 16, 17, 18], \
['Type B', 'Event1', 19, 20, 21], ['Type B', 'Event1', 22, 23, 24], ['Type B', 'Event1', 25, 26, 27], \
['Type B', 'Event2', 28, 29, 30], ['Type B', 'Event2', 31, 32, 33], ['Type B', 'Event2', 34, 35, 36]])

df.columns = ['TypeName', 'EventNumber', 'PricePart1', 'PricePart2', 'PricePart3']

print(df)

给予:

   TypeName EventNumber  PricePart1  PricePart2  PricePart3
0    Type A      Event1           1           2           3
1    Type A      Event1           4           5           6
2    Type A      Event1           7           8           9
3    Type A      Event2          10          11          12
4    Type A      Event2          13          14          15
5    Type A      Event2          16          17          18
6    Type B      Event1          19          20          21
7    Type B      Event1          22          23          24
8    Type B      Event1          25          26          27
9    Type B      Event2          28          29          30
10   Type B      Event2          31          32          33
11   Type B      Event2          34          35          36

这是我尝试过的:

df['Average'] = df[['PricePart1', 'PricePart2', 'PricePart3']].mean(axis = 1)

print(df)

       TypeName EventNumber  PricePart1  PricePart2  PricePart3  Average
0    Type A      Event1           1           2           3      2.0
1    Type A      Event1           4           5           6      5.0
2    Type A      Event1           7           8           9      8.0
3    Type A      Event2          10          11          12     11.0
4    Type A      Event2          13          14          15     14.0
5    Type A      Event2          16          17          18     17.0
6    Type B      Event1          19          20          21     20.0
7    Type B      Event1          22          23          24     23.0
8    Type B      Event1          25          26          27     26.0
9    Type B      Event2          28          29          30     29.0
10   Type B      Event2          31          32          33     32.0
11   Type B      Event2          34          35          36     35.0

现在我有了名为 Average 的新列,我可以按 TypeNameEventNumber 列进行分组,并使用以下命令查找第 25 个和第 50 个百分位数这段代码:

print(df.groupby(['TypeName', 'EventNumber'])['Average'].quantile([0.25, 0.50]).reset_index())

我有:

  TypeName EventNumber  level_2  Average
0   Type A      Event1     0.25      3.5
1   Type A      Event1     0.50      5.0
2   Type A      Event2     0.25     12.5
3   Type A      Event2     0.50     14.0
4   Type B      Event1     0.25     21.5
5   Type B      Event1     0.50     23.0
6   Type B      Event2     0.25     30.5
7   Type B      Event2     0.50     32.0

我希望将 level_2 作为单独的列,其中包含 Average 列中的值,就像我创建的输出 DataFrame 一样:

df1 = pd.DataFrame([['Type A', 'Event1', 3.5, 5], ['Type A', 'Event2', 12.5, 14], ['Type B', 'Event1', 21.5, 23], ['Type B', 'Event2', 30.5, 32]])
df1.columns = ['TypeName', 'EventNumber', '0.25', '0.50']
print(df1)

我想要什么:

  TypeName EventNumber  0.25  0.50
0   Type A      Event1   3.5     5
1   Type A      Event2  12.5    14
2   Type B      Event1  21.5    23
3   Type B      Event2  30.5    32

我非常确定这是重复的,但我已经在 StackOverflow 中进行了搜索,但没有找到答案,因为问题的措辞很困难(或者可能只是我很愚蠢)

最佳答案

使用unstackreset_index :

df = (df.groupby(['TypeName', 'EventNumber'])['Average']
       .quantile([0.25, 0.50])
       .unstack()
       .reset_index())
print (df)

  TypeName EventNumber  0.25   0.5
0   Type A      Event1   3.5   5.0
1   Type A      Event2  12.5  14.0
2   Type B      Event1  21.5  23.0
3   Type B      Event2  30.5  32.0

语法糖解决方案 - 新列Average不是必需的,可以将groupby3 Series一起使用:

s = df[['PricePart1', 'PricePart2', 'PricePart3']].mean(axis = 1)

df = (s.groupby([df['TypeName'], df['EventNumber']])
       .quantile([0.25, 0.50])
       .unstack()
       .reset_index())
print (df)

  TypeName EventNumber  0.25   0.5
0   Type A      Event1   3.5   5.0
1   Type A      Event2  12.5  14.0
2   Type B      Event1  21.5  23.0
3   Type B      Event2  30.5  32.0

关于python - 按两列分组并将不同的分位数打印为单独的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51378987/

相关文章:

Python查找两个不同长度的数据框列的部分匹配

python - 使用 Openpyxl 将单元格分配给变量

python 在替换中需要一个整数

python - ImportError:无法从部分初始化的模块 'main' 导入名称 ' '(很可能是由于循环导入)

python-3.x - MacOS 上 python-crfsuite 安装错误

python - Pandas 数据透视表中的小计

python - Octave 测试模块

python - 如何在python3中使用模块来赋予值(value)

python - Tensorflow 的多边形边界框

python - 将不同维度的 pandas 数据框相乘