Skip to content

Commit ddddabb

Browse files
authored
more pythonic answer
more pythonic answer to guipsamora#8. uses .resample method.
1 parent baf920a commit ddddabb

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

04_Apply/US_Crime_Rates/Exercises_with_solutions.ipynb

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -720,21 +720,10 @@
720720
],
721721
"source": [
722722
"# select the first two columns from the origional dataset\n",
723-
"population = crime.iloc[:, 0:1]\n",
724-
"\n",
725-
"# change the index for decades\n",
726-
"population.index = (population.index.year//10)*10\n",
727-
"\n",
728-
"# group the index by decades and selecting the max value from the Population column \n",
729-
"population1 = population.groupby(population.index).max()\n",
730-
"population1\n",
731-
"\n",
732-
"# group the dataframe by decades and sum all values, including population\n",
733-
"crime = crime.groupby((crime.index.year//10)*10).sum()\n",
734-
"\n",
735-
"# assing the right population value (max value) to the \n",
736-
"crime['Population'] = population1['Population']\n",
737-
"crime"
723+
"crimes = crime.resample('10AS').sum()\n",
724+
"population = crime['Population'].resample('10AS').max()\n",
725+
"crimes['Population'] = population\n",
726+
"crimes"
738727
]
739728
},
740729
{

0 commit comments

Comments
 (0)