分組后,默認會把分組的那列當作索引,如果不想這列當作索引,就可以添加參數如下,設置分組列不要作為索引:
grouby()函數可以按照某列分組,其中分組后的某列連成一個數組
增加新列:
https://www.cnpython.com/qa/598149
gg = df.groupby(by=['counterparty_id', 'cva_or_hedge', 'currency'],as_index=False)[['tenor', 'sensitivity']].apply(
lambda x: pd.Series(x.values.T.tolist(), x.columns))
gg['sensitivity'] = gg['sensitivity'].apply(lambda x :list(x+[0]*(6-len(x))))