【发布时间】:2018-03-21 13:08:00
【问题描述】:
所以,我有一个包含 3 列的数据框,每列有 631 行,所以我只突出显示每列下的唯一值。
df
Segment Type Nature of Query Q1
PRIME Request 1
BUSINESS Complaint 2
PRIORITY Critical Request 3
4
5
现在,假设在“Segment Type”下,我想将“PRIME”与“NATURE OF QUERY”和“Q1”的每一行分组并找到大小、最小值、最大值、平均值
所以尝试使用 groupby func,我得到了这个:
df.groupby(['Segment Type','Nature of Query'])['Q1'].agg([pd.np.size,
pd.np.min, pd.np.max, pd.np.mean])
而且,我得到了这个:
Segment Type Nature of Query size amin amax mean
BUSINESS Request 1 4 4 4.000000
PRIME Complaint 1 5 5 5.000000
Critical Request 3 1 2 1.666667
Request 31 1 5 3.387097
PRIORITY Critical Request 1 4 4 4.000000
Request 3 3 5 4.000000
我想要的输出:
Segment Type Nature of Query size amin amax mean
BUSINESS Request 1 4 4 4.000000
Complaint 1 5 5 5.000000
Critical Request 3 1 2 1.666667
PRIME Complaint 1 5 5 5.000000
Critical Request 3 1 2 1.666667
Request 31 1 5 3.387097
PRIORITY Complaint 1 5 5 5.000000
Critical Request 1 4 4 4.000000
Request 3 3 5 4.000000
忽略第一季度计算的大小、平均值、最大值等。我的主要问题是“Segment Type”和“Nature of Query”的值。
如果有任何可能的解决方案,请告诉我。 谢谢!
【问题讨论】:
-
您确定您使用的数据支持您想要的输出吗?
-
应该。
标签: python pandas pandas-groupby