[英]How to do groupby concat sum of only positives numbers in pandas dataframe
I have dataframe like as show where I need create a new data frame by grouping the input dataframe by document number and concat all the descriptions(column name:Text)with space delimiter and sum up the positive amounts as a new column.我有 dataframe ,如图所示,我需要通过按文档编号对输入 dataframe 进行分组来创建新数据框,并将所有描述(列名:文本)与空格分隔符相加,并将正数汇总为新列。
Input dataframe输入 dataframe
df
Doc Number Text Amount
122 DB1 25
122 DB2 25
122 DB3 -50
345 DB4 10
345 DB5 15
345 DB6 30
345 DB7 -55
Expectected Output预期 Output
New dataframe (Df1)全新 dataframe (Df1)
Doc Number Text Amount
122 DB1 DB2 DB3 50
345 DB4 DB5 DB6 DB7 55
My code till now我的代码到现在
df.groupby('Doc Number').apply(lambda x: ' '.join(x.Text))
How can this this be done?这怎么能做到呢?
Use pandas.DataFrame.groupby.agg
:使用
pandas.DataFrame.groupby.agg
:
new_df = df.groupby('Doc Number', as_index=False).agg({'Text': ' '.join, 'Amount': lambda x: sum(i for i in x if i > 0)})
print(new_df)
Output: Output:
Doc Number Amount Text
0 122 50 DB1 DB2 DB3
1 345 55 DB4 DB5 DB6 DB7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.