1

I want to do a subtraction of two columns of the same dataframe, col1 the row should be in a position [i+1] and the col2 in position [I]. (col 1 is the successor of col2).

The part of code that I can share, issue on the last return:

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')
Youness Saadna
  • 792
  • 2
  • 8
  • 25
Intissar
  • 13
  • 4
  • does your dataframe has index label? Loc works based on labels. For instance, if i==5, it looks for the index label equal to five. If you want the normal indexing (like a list index) you should use iloc – pooya Aug 11 '20 at 09:51
  • yes i've tried that , it gives me another error .I don't know if there is another way to do it tried using ix too : 'code' return df.iloc[i+1,10]-df.iloc[i,13] IndexingError: ('Too many indexers', 'occurred at index 593484569.0') – Intissar Aug 11 '20 at 09:55
  • and are you sure the dataframe indexing starts from 0? sometimes the "index label" has different values. For instance, if dataframe index starts from 5, df.loc[2] does not work but df.iloc[2] returns the third row – pooya Aug 11 '20 at 09:58
  • you can try to use df.iterrows instead of for loop to avoid these situations. https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas – pooya Aug 11 '20 at 10:03
  • Thank you for your response ,no @pooya i'm not sure the start of the indexing how can i verify that – Intissar Aug 11 '20 at 10:24
  • you can use df.index – pooya Aug 11 '20 at 10:28

1 Answers1

0

Try df['col3'] = df['col1'].shift(-1) - df['col2']

Rajesh
  • 766
  • 5
  • 17