Counting and grouping - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Counting and grouping (/thread-24001.html) |
Counting and grouping - Kudzo - Jan-27-2020 Hi, I've got a dataset and would like to add the number of cases per year and put it in a new column. I used the following codes but wasn't able to get the total of 'complaints' for each year. my_data_c = my_data[['created_date', 'complaint_type', 'incident_zip', 'latitude', 'longitude']] my_data_comp = my_data_c.groupby(['complaint_type']).sum() my_data_comp = my_data_comp.reset_index() my_data_comp.loc[:, 'borough'] = my_data.loc[:,'borough'] my_data_comp.loc[:, 'created_date'] = my_data.loc[:, 'created_date'] my_data_comp['no_of_complaints'] = my_data_no_incidents my_data_comp.head()Lest, I forget, I split the 'created_date' column into year, month and day. A screenshot of the original (part of) is added. Thanks in advance |