Question: Group a dataset through python. for eg. these 4 rows : REPORT_DAT HOOD_140 REPORT_DOW DIVISION LOCATION_TYPE PREMISES_TYPE OFFENCE Neighbourhood Count_of_police_force Average Individual Employment Income Population

Group a dataset through python.

for eg. these 4 rows :

REPORT_DAT HOOD_140 REPORT_DOW DIVISION LOCATION_TYPE PREMISES_TYPE OFFENCE Neighbourhood Count_of_police_force Average Individual Employment Income Population Unemployment Rate Low Income population(LIM-AT) EVENT_UNIQUE_ID
2018-01-01 76 Monday D52 Commercial Dwelling Unit (Hotel, Motel, B & B, Short Term Rental) Commercial Assault Bay Street Corridor 259 63,009 25,797 10.2 9,665 1
2018-01-01 76 Monday D52 Other Commercial / Corporate Places (For Profit, Warehouse, Corp. Bldg Commercial Assault Bay Street Corridor 259 63,009 25,797 10.2 9,665 1
2018-01-02 76 Tuesday D52 Apartment (Rooming House, Condo) Apartment Assault Bay Street Corridor 259 63,009 25,797 10.2 9,665 1
2018-01-02 76 Tuesday D52 Streets, Roads, Highways (Bicycle Path, Private Road) Outside Assault Bay Street Corridor 259 63,009 25,797 10.2 9,665 1

I want to group the 2 dates 1st jan had sum(event_unique_id) =2 , same way 2nd jan had sum(event_unique_id) =2.

i use this code :

grouped_crimedf= crime_df.groupby(['HOOD_140','REPORT_DAT','REPORT_DOW','DIVISION','LOCATION_TYPE','PREMISES_TYPE','OFFENCE','Neighbourhood','Count_of_police_force','Average Individual Employment Income','Population','Unemployment Rate','Low Income population(LIM-AT)']).agg({'EVENT_UNIQUE_ID': 'count'}).reset_index()


I have to use all the columns because if i drop some of them from the above statement then i dont get them in the grouped_df dataset which means i cannot apply LR later on on these columns. how can i get all the columns and at the same time group it based on only date and hood_140.

Step by Step Solution

3.54 Rating (151 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Improved code import pandas as pd Assuming your data is in a list of lists or a DataFrame named crimedf data 20180101 Monday 25963 Your data rows here ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!