累积值与 groupby 列的第一个条目相加

Cumulative Value summed up with first entry of a column with groupby

我得到了以下数据框:

lst=[['01012021','A',100,'NaN'],['01012021','B',120,'NaN'],['01022021','A',140,5],['01022021','B',160,12],['01032021','A',180,20],['01032021','B',200,25]]
df1=pd.DataFrame(lst,columns=['Date','FN','AuM','NNA'])

我想生成一个新列 (AuM_2),它采用 AuM 列中的起始值,并通过 Product 列累积 NNA 列中的条目。最终结果应该类似于下面提到的数据框:

lst=[['01012021','A',100,'NaN','NaN'],['01012021','B',120,'NaN','NaN'],['01022021','A',140,5,105],['01022021','B',160,12,132],['01032021','A',180,20,125],['01032021','B',200,25,157]]
df2=pd.DataFrame(lst,columns=['Date','FN','AuM','NNA','AuM_2'])

您知道如何使用 groupby 生成列 AuM_2 吗? 感谢您的协助。

GroupBy.cumsum and first values per groups by GroupBy.transform 中的值求和:

df1['NNA'] = pd.to_numeric(df1['NNA'], errors='coerce')
df1['AuM'] = pd.to_numeric(df1['AuM'], errors='coerce')

g = df1.groupby('FN')
df1['AuM_2'] = g['NNA'].cumsum() + g['AuM'].transform('first')
print (df1)
       Date FN  AuM   NNA  AuM_2
0  01012021  A  100   NaN    NaN
1  01012021  B  120   NaN    NaN
2  01022021  A  140   5.0  105.0
3  01022021  B  160  12.0  132.0
4  01032021  A  180  20.0  125.0
5  01032021  B  200  25.0  157.0