计算DataFrame的百分比

时间:2018-06-13 07:18:26

标签: python pandas dataframe pandas-groupby

我试图计算以下数据框中每种犯罪的百分比:

        Violent     Murder      Larceny_Theft   Vehicle_Theft
Year
1960    288460      3095700     1855400         328200
1961    289390      3198600     1913000         336000
1962    301510      3450700     2089600         366800
1963    316970      3792500     2297800         408300
1964    364220      4200400     2514400         472800

因此,我应首先计算每年的犯罪总数,然后用它来计算每种犯罪的百分比。我正在尝试以下方法:

> perc = (crime *100) / crime.sum(axis=1)

有什么想法? 谢谢!

1 个答案:

答案 0 :(得分:4)

使用函数DataFrame.div除以<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script> </head> <body ng-controller="myCtrl"> <div class="container"> <form action="" class=""> <div id="widgetContainer"> <!--<input type="text" ng-click="openSelect()" class="input-control"> <div id="selectContainer" ng-show="selectEnable"> <div>{{selectedItems.toString()}}</div> <input type="text" id="searchField" ng-model="searchField" ng-change="filter()"> <div id="listContainer"> <ul id="innerContainer"> <li ng-repeat="data in data2Show | orderBy: data.item" ng-model="data2show"> <h4>{{data.category}}</h4> <input type="checkbox" ng-change="itemChecked(data)" name="select" ng-model="data.flag" ng-checked="isChecked(data)"> {{data.item}} </li> <div ng-show="dataLoading" ng-model="dataLoading">loading...</div> <li id="loadMore" ng-click="loadMore()"> load more </li> </ul> </div> </div>--> <ul id="select"> <li ng-repeat="data in categorizedData" class="isDisabled">{{data.item }} <ul ng-if="data.items && data.items.length > 0"> <li ng-repeat="itemVar in data.items" class="isDisabled">{{itemVar.item }}</li> </ul> </li> </ul> </div> </form> </div> </body> </html>

Series