|
719 | 719 | } |
720 | 720 | ], |
721 | 721 | "source": [ |
722 | | - "# select the first two columns from the origional dataset\n", |
| 722 | + "# To learn more about .resample (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html)\n", |
| 723 | + "# To learn more about Offset Aliases (http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases)\n", |
| 724 | + "\n", |
| 725 | + "# Uses resample to sum each decade\n", |
723 | 726 | "crimes = crime.resample('10AS').sum()\n", |
| 727 | + "\n", |
| 728 | + "# Uses resample to get the max value only for the \"Population\" column\n", |
724 | 729 | "population = crime['Population'].resample('10AS').max()\n", |
| 730 | + "\n", |
| 731 | + "# Updating the \"Population\" column\n", |
725 | 732 | "crimes['Population'] = population\n", |
| 733 | + "\n", |
726 | 734 | "crimes" |
727 | 735 | ] |
728 | 736 | }, |
|
765 | 773 | "# apparently the 90s was a pretty dangerous time in the US\n", |
766 | 774 | "crime.idxmax(0)" |
767 | 775 | ] |
768 | | - }, |
769 | | - { |
770 | | - "cell_type": "markdown", |
771 | | - "metadata": {}, |
772 | | - "source": [ |
773 | | - "### Step 10. Find out which Year over Year delta was " |
774 | | - ] |
775 | | - }, |
776 | | - { |
777 | | - "cell_type": "code", |
778 | | - "execution_count": null, |
779 | | - "metadata": { |
780 | | - "collapsed": false |
781 | | - }, |
782 | | - "outputs": [], |
783 | | - "source": [] |
784 | | - }, |
785 | | - { |
786 | | - "cell_type": "markdown", |
787 | | - "metadata": {}, |
788 | | - "source": [ |
789 | | - "### Step 11. How many items were orderd in total?" |
790 | | - ] |
791 | | - }, |
792 | | - { |
793 | | - "cell_type": "code", |
794 | | - "execution_count": 279, |
795 | | - "metadata": { |
796 | | - "collapsed": false |
797 | | - }, |
798 | | - "outputs": [ |
799 | | - { |
800 | | - "ename": "AttributeError", |
801 | | - "evalue": "'int' object has no attribute 'max'", |
802 | | - "output_type": "error", |
803 | | - "traceback": [ |
804 | | - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
805 | | - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", |
806 | | - "\u001b[0;32m<ipython-input-279-dc545c79125f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mcrime\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Violent'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;31m# crime.apply(f, axis=1)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
807 | | - "\u001b[0;32m//anaconda/lib/python2.7/site-packages/pandas/core/series.pyc\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, convert_dtype, args, **kwds)\u001b[0m\n\u001b[1;32m 2218\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2219\u001b[0m \u001b[0mvalues\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masobject\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2220\u001b[0;31m \u001b[0mmapped\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmap_infer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconvert\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mconvert_dtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2221\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2222\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmapped\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmapped\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mSeries\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
808 | | - "\u001b[0;32mpandas/src/inference.pyx\u001b[0m in \u001b[0;36mpandas.lib.map_infer (pandas/lib.c:62658)\u001b[0;34m()\u001b[0m\n", |
809 | | - "\u001b[0;32m<ipython-input-279-dc545c79125f>\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(x)\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mcrime\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Violent'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# crime.apply(f, axis=1)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
810 | | - "\u001b[0;31mAttributeError\u001b[0m: 'int' object has no attribute 'max'" |
811 | | - ] |
812 | | - } |
813 | | - ], |
814 | | - "source": [ |
815 | | - "f = lambda x: x.max() - x.min()\n", |
816 | | - "\n", |
817 | | - "crime['Violent'].apply(f)\n", |
818 | | - "# crime.apply(f, axis=1)" |
819 | | - ] |
820 | | - }, |
821 | | - { |
822 | | - "cell_type": "markdown", |
823 | | - "metadata": {}, |
824 | | - "source": [ |
825 | | - "### Step 12. How many orders have more than 1 item?" |
826 | | - ] |
827 | | - }, |
828 | | - { |
829 | | - "cell_type": "code", |
830 | | - "execution_count": null, |
831 | | - "metadata": { |
832 | | - "collapsed": false |
833 | | - }, |
834 | | - "outputs": [], |
835 | | - "source": [] |
836 | | - }, |
837 | | - { |
838 | | - "cell_type": "markdown", |
839 | | - "metadata": {}, |
840 | | - "source": [ |
841 | | - "### Step 13. How much was the revenue for the period in the dataset?" |
842 | | - ] |
843 | | - }, |
844 | | - { |
845 | | - "cell_type": "code", |
846 | | - "execution_count": null, |
847 | | - "metadata": { |
848 | | - "collapsed": false |
849 | | - }, |
850 | | - "outputs": [], |
851 | | - "source": [] |
852 | | - }, |
853 | | - { |
854 | | - "cell_type": "markdown", |
855 | | - "metadata": {}, |
856 | | - "source": [ |
857 | | - "### Step 14. How many orders were made in the period?" |
858 | | - ] |
859 | | - }, |
860 | | - { |
861 | | - "cell_type": "code", |
862 | | - "execution_count": null, |
863 | | - "metadata": { |
864 | | - "collapsed": true |
865 | | - }, |
866 | | - "outputs": [], |
867 | | - "source": [] |
868 | | - }, |
869 | | - { |
870 | | - "cell_type": "markdown", |
871 | | - "metadata": {}, |
872 | | - "source": [ |
873 | | - "### Step 15. What is the average amount per order?" |
874 | | - ] |
875 | | - }, |
876 | | - { |
877 | | - "cell_type": "code", |
878 | | - "execution_count": null, |
879 | | - "metadata": { |
880 | | - "collapsed": true |
881 | | - }, |
882 | | - "outputs": [], |
883 | | - "source": [] |
884 | | - }, |
885 | | - { |
886 | | - "cell_type": "markdown", |
887 | | - "metadata": {}, |
888 | | - "source": [ |
889 | | - "### Step 16. How many different items are sold?" |
890 | | - ] |
891 | | - }, |
892 | | - { |
893 | | - "cell_type": "code", |
894 | | - "execution_count": null, |
895 | | - "metadata": { |
896 | | - "collapsed": true |
897 | | - }, |
898 | | - "outputs": [], |
899 | | - "source": [] |
900 | 776 | } |
901 | 777 | ], |
902 | 778 | "metadata": { |
| 779 | + "anaconda-cloud": {}, |
903 | 780 | "kernelspec": { |
904 | | - "display_name": "Python 2", |
| 781 | + "display_name": "Python [default]", |
905 | 782 | "language": "python", |
906 | 783 | "name": "python2" |
907 | 784 | }, |
|
915 | 792 | "name": "python", |
916 | 793 | "nbconvert_exporter": "python", |
917 | 794 | "pygments_lexer": "ipython2", |
918 | | - "version": "2.7.11" |
| 795 | + "version": "2.7.12" |
919 | 796 | } |
920 | 797 | }, |
921 | 798 | "nbformat": 4, |
|
0 commit comments