The problem is that the dtype
of the Series for "ProfitLoss" is inferred from the original data, i.e. string. You need to either set the Series to float, or set numeric_only=False
in sum()
.
That is, either changing the dtype
of "ProfitLoss"
dat = dat.astype({'ProfitLoss': 'float'})
market_groups = dat.groupby(['MarketName']).sum()
Or setting numeric_only to False
market_groups = dat.groupby(['MarketName']).sum(numeric_only= False)
should work.
Also wanted to point out that the -1
in dat['ProfitLoss'][index] = float(dat['ProfitLoss'][index][1:length-1])
should probably not be there? I think you meant this to remove the ending bracket )
but you don't need to do that in the else clause.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…