[英]pandas dataframe: how to aggregate a subset of rows based on value of a column
I have a pandas dataframe structured like this:我有一个如下结构的熊猫数据框:
value
lab
A 50
B 35
C 8
D 5
E 1
F 1
This is just an example, the actual dataframe is bigger, but follows the same structure.这只是一个例子,实际的数据帧更大,但遵循相同的结构。
The sample dataframe has been created with this two lines:示例数据框已使用以下两行创建:
df = pd.DataFrame({'lab':['A', 'B', 'C', 'D', 'E', 'F'], 'value':[50, 35, 8, 5, 1, 1]})
df = df.set_index('lab')
I would like to aggregate the rows whose value is smaller that a given threshold: all these rows should be substituted by a single row whose value is the sum of the substituted rows.我想聚合其值小于给定阈值的行:所有这些行都应该被一行替换,该行的值是替换行的总和。
For example, if I choose a threshold = 6, the expected result should be the following:例如,如果我选择阈值 = 6,则预期结果应如下所示:
value
lab
A 50
B 35
C 8
X 7 #sum of D, E, F
How can I do this?我怎样才能做到这一点?
I thought to use groupby()
, but all the examples I've seen involved the use of a separate column for grouping, so I do not know how to use it in this case.我想使用
groupby()
,但是我看到的所有示例都涉及使用单独的列进行分组,所以我不知道在这种情况下如何使用它。
I can select the rows smaller than my threshold with loc
, by doing df.loc[df['value'] < threshold]
but I do not know how to sum only these rows and leave the rest of the dataframe unaltered.通过执行
df.loc[df['value'] < threshold]
我可以使用loc
选择小于我的阈值的行,但我不知道如何仅对这些行求和而保留数据帧的其余部分不变。
You can use lambda
and DataFrame.append
to achieve this in a 'one-liner':您可以使用
lambda
和DataFrame.append
在“ DataFrame.append
中实现这一点:
thresh = 6
(df[lambda x: x['value'] >= thresh]
.append(df[lambda x: x['value'] < thresh].sum().rename('X')))
Or if you prefer或者如果你喜欢
mask = df['value'].ge(thresh)
df[mask].append(df[~mask].sum().rename('X'))
[out] [出去]
value
lab
A 50
B 35
C 8
X 7
Use setting with enlargement with filtered DataFrame
:使用带有过滤的
DataFrame
放大设置:
threshold = 6
m = df['value'] < threshold
df1 = df[~m].copy()
df1.loc['Z'] = df.loc[m, 'value'].sum()
print (df1)
value
lab
A 50
B 35
C 8
Z 7
Another solution:另一种解决方案:
m = df['value'] < threshold
df1 = df[~m].append(df.loc[m, ['value']].sum().rename('Z'))
print (df1)
value
lab
A 50
B 35
C 8
Z 7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.