import pandas as pd
df1 = pd.DataFrame({'ds': [20200601, 20200601, 20200601, 20200601, 20200601,
20200601, 20200601, 20200601],
'code': ['000001.SZ', '000002.SZ', '000004.SZ', '000005.SZ',
'000006.SZ', '000007.SZ', '000008.SZ', '000009.SZ'],
'ratio': [1.64, 3.96, 0.00, 0.00, 3.68, 0.00, 0.33, 0.28]})
df1 = pd.DataFrame(df1.groupby(['ds','code'])['ratio'].apply(sum))
df2 = pd.DataFrame({'ds': [20200601, 20200601, 20200601, 20200601, 20200601],
'code': ['000001.SZ', '000002.SZ', '000004.SZ', '000008.SZ',
'000009.SZ']}).set_index('ds')
result = df1.reset_index().merge(df2.reset_index(), how='inner', left_on=['ds','code'], right_on=['ds','code'])
result = pd.DataFrame(result.groupby(['ds','code'])['ratio'].apply(sum))
print(result)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…