I have a series grouped on districts -> crime types -> count of crimes:
PdDistrict Category
BAYVIEW ASSAULT 8976
BURGLARY 2891
DISORDERLY CONDUCT 207
DRIVING UNDER THE INFLUENCE 188
DRUG/NARCOTIC 2061
...
TENDERLOIN STOLEN PROPERTY 299
TRESPASS 665
VANDALISM 1710
VEHICLE THEFT 661
WEAPON LAWS 791
Name: IncidntNum, Length: 140, dtype: int64
My goal is to divide every value with a scalar.
I tried to do this using a loop going over the "PdDistricts" and run the following line:
series[district] = series[district] / sum(series[district])
If i run just series[district] / sum(series[district])
the output is as intended:
Category
ASSAULT 0.11434063
BURGLARY 0.09323762
DISORDERLY CONDUCT 0.00427552
DRIVING UNDER THE INFLUENCE 0.00478544
DRUG/NARCOTIC 0.05691535
DRUNKENNESS 0.00596219
LARCENY/THEFT 0.46712952
PROSTITUTION 0.00027457
ROBBERY 0.02753589
STOLEN PROPERTY 0.00917863
TRESPASS 0.01247352
VANDALISM 0.09335530
VEHICLE THEFT 0.09884679
WEAPON LAWS 0.01168902
Name: IncidntNum, dtype: float64
But when I try to update the existing part of the series running series[district] = series[district] / sum(series[district])
i get:
Category
ASSAULT 0
BURGLARY 0
DISORDERLY CONDUCT 0
DRIVING UNDER THE INFLUENCE 0
DRUG/NARCOTIC 0
DRUNKENNESS 0
LARCENY/THEFT 0
PROSTITUTION 0
ROBBERY 0
STOLEN PROPERTY 0
TRESPASS 0
VANDALISM 0
VEHICLE THEFT 0
WEAPON LAWS 0
Name: IncidntNum, dtype: int64
Which is not as intended. If I use .loc I simply get NaN's instead of 0's.
I simply can't wrap my head around what's going wrong, all solutions I have tried have failed, and I think the key issue is that I do not fully understand how to work with Series in Pandas.
I hope you can help me understand the issue.
/Mikkel