pandas交差、差分、並列

1166 ワード

詳細
      python 3はpandasを用いてデータの統合を行う.交差、差分、並列を取る必要がある場合があります.
 
 
# coding:utf-8

import nothing as my_project
import pandas as pd


def get_data():
    file_1 = my_project.project_root + '/resources/' + '1_uniq.txt'
    file_2 = my_project.project_root + '/resources/' + '2_uniq.txt'

    #       
    order_id_me = pd.read_table(file_1, names=['order_id'])
    order_id_me['v1'] = order_id_me.apply(lambda x: 1, axis=1)
    order_id_you = pd.read_table(file_2, names=['order_id'])
    order_id_you['v2'] = order_id_you.apply(lambda x: 2, axis=1)

    #   
    union_set = pd.merge(order_id_you, order_id_me, how='outer')
    #   
    difference_set = union_set[(union_set['v1'].isnull()) | (union_set['v2'].isnull())]
    #   
    intersection_set = union_set[(union_set['v1'].notnull()) & (union_set['v2'].notnull())]

 
     以上のコードは特定の場合にのみ適用され,merge関数のhowパラメータを柔軟に運用することを主旨とする.