PythonはDataframeの対応する列によって差と変化率を求める


1(今日)DataFrame形式を入力:
  code date_time
0   10      1100
1   20      2200
2   30      3300
3   40      4400

2(昨日)DataFrame形式を入力:
  code date_time
0   10      1000
1   20      2500
2   50      5000

実装機能:
あるキーワードの列に基づいて、今日の表のある列の変化率を計算します.公式は(今日頻度-昨日頻度)/昨日頻度です.
昨日現れず、分母が0の場合、変化率は1に設定されます.
希望出力、DataFrame形式:
  code  date_time_x  date_time_y      
0   10         1100         1000  0.10
1   20         2200         2500 -0.12
2   30         3300            0  1.00
3   40         4400            0  1.00

Python実装コード:
import numpy as np
import pandas as pd

#     
dt1 = {'code': ['10', '20', '30', '40'], 'date_time': ['1100', '2200', '3300', '4400'], }
dataframe_today = pd.DataFrame(dt1)
print(dataframe_today)

dt2 = {'code': ['10', '20', '50'], 'date_time': ['1000', '2500', '5000']}
dataframe_yesterday = pd.DataFrame(dt2)
print(dataframe_yesterday)

#      
data_merge = pd.merge(dataframe_today, dataframe_yesterday, on='code', how='left')
data_merge.fillna(0, inplace=True)
data_merge[['date_time_x', 'date_time_y']] = data_merge[['date_time_x', 'date_time_y']].astype('int')
data_merge['   '] = (data_merge['date_time_x'] - data_merge['date_time_y']) / data_merge['date_time_y']
pd.Series(data_merge['   '])[np.isinf(data_merge['   '])] = 1
# data_merge = data_merge.sort_values(by='   ', ascending=False) #               
print(data_merge)