简体   繁体   English

pandas 数据框:如何根据列的值聚合行的子集

[英]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':您可以使用lambdaDataFrame.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.

相关问题 如何根据 Pandas 中的条件为 dataframe 子集的列分配值? - How to assign a value to a column for a subset of dataframe based on a condition in Pandas? 基于按字母顺序排列的列值的 Pandas 子集数据框 - Pandas subset dataframe based on column value that alphabetically 基于列值的熊猫子集和放置行 - pandas subset and drop rows based on column value 如何将 append 中的行子集聚合到多索引 Pandas DataFrame? - How to aggregate a subset of rows in and append to a MultiIndexed Pandas DataFrame? 根据其他列中的值,在列中 dataframe 行的子集上应用 function - apply function on subset of dataframe rows in column based on value in other column 在Pandas DataFrame上更新行子集的列值的有效方法? - Efficient way to update column value for subset of rows on Pandas DataFrame? 如何根据同一 pandas dataframe 中的列的值复制行 - How to replicate rows based on value of a column in same pandas dataframe 如何根据pandas数据框中的多列值条件排除行? - How to exclude rows based on multi column value conditions in pandas dataframe? 如何根据 Pandas dataframe 中的列值合并和居中两行? - How to merge and center two rows based on a column value in Pandas dataframe? 如何根据 Pandas 数据框中的列值(int)合并行(带字符串)? - How to merge rows (with strings) based on column value (int) in Pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM