I’m trying to count the number of each zip codes within a monthly period of time frame.
df1 = pd.DataFrame(df)
print(df[['Timestamp', 'zip']].nunique())
I get an output of:
Timestamp 8314
zip 343
dtype: int64
I don’t need the number of zip codes, I need the count of each zip code.
I was expecting:
60187 2
60542 1
60540 3
etc.
asked to post sample rows:
Timestamp zip
0 Front Desk Call Log (Master Data) NaN
1 2023-07-21 12:22:47.697000 60191
2 2023-07-21 10:55:13.311000 NaN
3 2023-07-21 10:49:06.148000 60187
4 2023-07-21 10:29:08.396000 60189
... ... ...
8309 2023-07-21 14:43:23.522000 60187
8310 2023-07-21 14:45:12.332000 60440
8311 2023-07-21 14:46:46.452000 NaN
8312 2023-07-21 17:34:11.631000 60548
8313 2023-07-21 17:39:36.358000 60133
[8314 rows x 2 columns]
>Solution :
An alternative solution to achieve the same result is by using the groupby function in combination with value_counts. Here’s the code for the alternate solution:
Group by ‘zip’ and count the occurrences of each zip code
df.groupby('zip').size()