减去同一数据框的两列

时间:2020-08-11 09:22:07

标签: python pandas dataframe jupyter-notebook calculated-columns

我想对同一数据帧的两列进行减法,col1该行应位于位置[i + 1],col2应位于位置[I]。 (col 1是col2的后继者。)

我可以共享的部分代码,在最后一次返回时发出:

def calcul_TAT2(df):
    i=0
    for i in (range(len(df))):
        if  type(df['actual_gate_arrival_time']) is float or type(df['actual_gate_departure_time']) is float:
            return 'NaN'
        if  df['actual_gate_arrival_time'].lower() =='nan' or df['actual_gate_departure_time'].lower()=='nan':
            return 'NaN'
        else :
            actual_gate_arrival_time= (datetime(int(df['actual_gate_arrival_time'][6:10]),int(df['actual_gate_arrival_time'][3:5]),int(df['actual_gate_arrival_time'][:2]),int(df['actual_gate_arrival_time'][11:13]),int(df['actual_gate_arrival_time'][14:])))
            actual_gate_departure_time= (datetime(int(df['actual_gate_departure_time'][6:10]),int(df['actual_gate_departure_time'][3:5]),int(df['actual_gate_departure_time'][:2]),int(df['actual_gate_departure_time'][11:13]),int(df['actual_gate_departure_time'][14:])))       
        return df.loc[i+1,'actual_gate_departure_time']-df.loc[i,'actual_gate_arrival_time'] 
DHA['TAT']=DHA.apply(calcul_TAT2, axis=1)

TypeError: ("cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [1] of <class 'int'>", 'occurred at index 593484569.0')

1 个答案:

答案 0 :(得分:0)

尝试class Commission: def __init__(self, call, permit, install): self.call = call self.permit = permit self.install = install def pay_pre_tax(self): calls = 300 permits = 200 installs = 500 self.total = calls * self.call + permits * self.permit + installs + self.install return self.total def taxes(self): self.tax_rate = 4.66 / self.total return self.tax_rate def total_pay(self): total = self.pay_pre_tax() total_pay = total + self.tax_rate return total_pay Pynchon = Commission(1, 2, 3) print(Pynchon.pay_pre_tax()) Pynchon.taxes() print(Pynchon.total_pay())