I have an array with postal codes and I want to build a dictionary with the postal codes and the result of a function related to them. (Basically the loop goes to a DataFrame and looks how many different 'CouncilArea' are there for each postal code).
This is the for loop i've tried:
for cps in codigos_postales:
codigo_postal_council = {}
codigo_postal_council[cps] = pd.notna(pd.unique(data[data['Postcode']==cps]['CouncilArea'])).sum()
The problem is that in the output I don't get the full dictionary with postal codes and results. I only get the last item of the array and the result of the function as output.
{3793.0: 0}
I know the function I coded is working properly because if i include this code in the loop I get a long print of results.
print(pd.notna(pd.unique(data[data['Postcode']==cps]['CouncilArea'])).sum())
I've searched the web for about and hour and still not getting a solution. Thank you very much for your kind support!!!
CodePudding user response:
More the dict creation outside loop
codigo_postal_council = {}
for cps in codigos_postales:
codigo_postal_council[cps] = pd.notna(pd.unique(data[data['Postcode']==cps]['CouncilArea'])).sum()