使用WHERE子句的JOIN的熊猫模拟

时间:2016-11-29 14:05:52

标签: python sql pandas

我正在加入python的pandas中的两个数据帧(A和B)。

目标是从B接收所有纯行(sql analogue-右连接B,在A.client_id = B.client_id,其中A.client_id为空)

在大熊猫中我所知道的这个操作是合并但我不知道如何设置条件(where子句):

angular.module('starter', ['ionic', 'starter.services', 'firebase', 'AccountKit'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      StatusBar.styleDefault();
    }
  })
})

// Start of Controller

.controller('LoginCtrl', function($scope){
  // initialize Account Kit with CSRF protection
  $scope.AccountKit_OnInteractive = function(response){
  AccountKit.init({
      appId:'secret', 
      state:"secret", 
      version:"v1.1"
  })
 }
})

.controller('DashCtrl', function($scope) {})

.controller('ChatsCtrl', function($scope, Chats) {
  $scope.chats = Chats.all();
  $scope.remove = function(chat) {
    Chats.remove(chat);
  };
})

.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
   $scope.chat = Chats.get($stateParams.chatId);
})

.controller('AccountCtrl', function($scope) {
   $scope.settings = {
   enableFriends: true
   };
});

// End of Controller

// Start Routing

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('login', {
    url: '/login',
    templateUrl: 'templates/login.html',
    controller: 'LoginCtrl'
  })

  // setup an abstract state for the tabs directive
  .state('tab', {
      url: '/tab',
      abstract: true,
      templateUrl: 'templates/tabs.html'
  })

  // Each tab has its own nav history stack:

 .state('tab.dash', {
      url: '/dash',
      views: {
         'tab-dash': {
         templateUrl: 'templates/tab-dash.html',
         controller: 'DashCtrl'
         }
      }
  })

 .state('tab.chats', {
     url: '/chats',
     views: {
    'tab-chats': {
         templateUrl: 'templates/tab-chats.html',
         controller: 'ChatsCtrl'
     }
   }
})
.state('tab.chat-detail', {
  url: '/chats/:chatId',
  views: {
    'tab-chats': {
      templateUrl: 'templates/chat-detail.html',
      controller: 'ChatDetailCtrl'
    }
  }
})

  .state('tab.account', {
    url: '/account',
    views: {
      'tab-account': {
        templateUrl: 'templates/tab-account.html',
        controller: 'AccountCtrl'
      }
    }
  })

   // if none of the above states are matched, use this as the fallback
   $urlRouterProvider.otherwise('/login');

});

// End of Routing

// Initialize Firebase
var config = {
  apiKey: "secret",
  authDomain: "secret",
  databaseURL: "secret",
  storageBucket: "secret",
  messagingSenderId: "secret"
};
firebase.initializeApp(config);

谢谢!

2 个答案:

答案 0 :(得分:5)

对我来说,这也有点令人不满意,但我认为建议的方式类似于for case in all_case: # I want an object to be newly created / reinstantiated in each loop pda = PushDownAutomata() print pda.evaluate(case, debug=False) # I already added "del pda" but it does not work

可以在pandas documentation

中找到更多信息

此外,您可以使用x=pd.merge(A[A["client_ID"].isnull()],B,how='right',on=['client_id','client_id'])之类的内容进行过滤。另外,请注意我之前版本中的错误。我正在与A.where(A["client_ID"].isnull())进行比较,但您应该使用None函数

答案 1 :(得分:4)

选项1
indicator=True

A.merge(B, on='client_id', how='right', indicator=True) \
    .query('_merge == "right_only"').drop('_merge', 1)

设置

A = pd.DataFrame(dict(client_id=[1, 2, 3], valueA=[4, 5, 6]))
B = pd.DataFrame(dict(client_id=[3, 4, 5], valueB=[7, 8, 9]))

结果

enter image description here

更多解释
indicator=True在合并结果中放入另一列,指示行结果是来自左,右还是两者。

A.merge(B, on='client_id', how='outer', indicator=True)

enter image description here

因此,我只是使用query过滤掉right_only指标,然后删除该列。

选项2
不是真的合并。您可以再次使用query来仅提取其B不在'client_id'

中的A
B.query('client_id not in @A.client_id')

或说同样的事情的同等方式(但更快)

B[~B.client_id.isin(A.client_id)]

enter image description here