python中条件的列总和

时间:2019-05-28 08:40:29

标签: pandas dataframe

我有一个数据框

id_command  command_status  stage   line_amount global_amount
61  ZeFMAA1 Pending     582.96  582.96
61  UbUjNAAV    Pending     70481.00    582.96
945 0bR8hEQAS   Pending     6400.00 12800.00

我想添加一个新的列“ decision”,它是一个布尔值(对或错) 如果命令的global_amount大于命令的line_amount的总和的30%,则为true;否则为false。

您能帮我做这个吗?

谢谢

2 个答案:

答案 0 :(得分:1)

使用:

df['decision'] = df['global_amount'].gt(df['line_amount'].mul(.3))
print (df)
   id_command command_status    stage  line_amount  global_amount  decision
0          61        ZeFMAA1  Pending       582.96         582.96      True
1          61       UbUjNAAV  Pending     70481.00         582.96     False
2         945      0bR8hEQAS  Pending      6400.00       12800.00      True

答案 1 :(得分:1)

基于:

  

一条命令的line_amount之和

使用df.groupby(){ "name": "kontrak-hukum-front-end", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e" }, "private": true, "dependencies": { "@agm/core": "^1.0.0-beta.5", "@angular/animations": "^8.0.0-rc.4", "@angular/common": "^8.0.0-rc.4", "@angular/compiler": "^8.0.0-rc.4", "@angular/core": "^8.0.0-rc.4", "@angular/forms": "^8.0.0-rc.4", "@angular/platform-browser": "^8.0.0-rc.4", "@angular/platform-browser-dynamic": "^8.0.0-rc.4", "@angular/platform-server": "^8.0.0-rc.4", "@angular/router": "^8.0.0-rc.4", "@fortawesome/angular-fontawesome": "^0.3.0", "@ng-bootstrap/ng-bootstrap": "^4.1.3", "@ng-select/ng-select": "^2.20.0", "@nicky-lenaers/ngx-scroll-to": "^2.0.0", "@swimlane/ngx-datatable": "^14.0.0", "@types/googlemaps": "^3.36.0", "angular-2-local-storage": "^3.0.2", "angular-froala-wysiwyg": "^3.0.0-beta.1", "angular-io-slimscroll": "^2.3.3", "angular2-chartjs": "^0.5.1", "angular2-datatable": "^0.6.0", "angular2-material-datepicker": "^0.5.0", "angular2-notifications": "^2.0.0", "angular2-wizard": "^0.4.0", "bootstrap": "^4.3.1", "c3": "^0.7.0", "core-js": "^3.1.2", "css-animator": "^2.3.1", "d3": "^5.9.2", "echarts": "^4.2.1", "famfamfam-flags": "^1.0.0", "font-awesome": "^4.7.0", "jquery": "^3.4.1", "jquery-sparkline": "^2.4.0", "ng-auto-complete": "^4.1.7", "ng-click-outside": "^4.0.0", "ng-snotify": "^4.3.1", "ng2-auto-complete": "^0.12.0", "ng2-ckeditor": "^1.2.2", "ng2-currency-mask": "^5.3.1", "ng2-file-upload": "^1.3.0", "ng2-google-charts": "^4.0.0", "ng2-nvd3": "^2.0.0", "ng2-order-pipe": "^0.1.5", "ng2-search-filter": "^0.5.1", "ng2-toasty": "^4.0.3", "ng2-ui-switch": "^1.0.2", "ng2-validation": "^4.2.0", "ngx-bootstrap": "^4.3.0", "ngx-chips": "^2.0.0-beta.0", "ngx-color-picker": "^7.5.0", "ngx-echarts": "^4.1.1", "ngx-order-pipe": "^2.0.2", "ngx-pagination": "^3.2.1", "ngx-quill": "^5.1.0", "ngx-quill-editor": "^2.2.2", "ngx-webstorage": "^3.0.2", "npm": "^6.9.0", "peity": "^3.3.0", "rxjs": "^6.5.2", "screenfull": "^4.2.0", "squeezebox": "^1.4.1", "sweetalert2": "^8.11.4", "ti-icons": "^0.1.2", "tslib": "^1.9.0", "wijmo": "^5.20191.606", "zone.js": "~0.9.1" }, "devDependencies": { "@angular-devkit/build-angular": "~0.800.0-rc.4", "@angular/cli": "^8.0.0-rc.4", "@angular/compiler-cli": "^8.0.0-rc.4", "@angular/language-service": "^8.0.0-rc.4", "@types/jasmine": "~3.3.12", "@types/jasminewd2": "~2.0.3", "@types/node": "~12.0.2", "codelyzer": "^5.0.1", "jasmine-core": "~3.4.0", "jasmine-spec-reporter": "~4.2.1", "karma": "~4.1.0", "karma-chrome-launcher": "~2.2.0", "karma-coverage-istanbul-reporter": "~2.0.1", "karma-jasmine": "~2.0.1", "karma-jasmine-html-reporter": "^1.4.2", "protractor": "~5.4.0", "ts-node": "~8.1.0", "tslint": "~5.11.0", "typescript": "3.4.5" } } 进行分组,并使用id_command获取每一行的总和。然后乘以transform,然后乘以series.gt()

0.3

df['decision']=df.global_amount.gt(df.groupby('id_command').line_amount.transform('sum')*0.3)
print(df)