1

is there any faster way to this code? i just want to calculate t_last - t_i and create a new column

time_ges = pd.DataFrame()
for i in range(0, len(df.GesamteMessung_Sec.index), 1):
    time = df.GesamteMessung_Sec.iloc[-1]-df.GesamteMessung_Sec.iloc[i]
    time_ges = time_ges.append(pd.DataFrame({'echte_Ladezeit': time}, index=[0]), ignore_index=True)

df['echte_Ladezeit'] = time_ges

this code takes a lot of computation time, is there any better way to do this? thanks, R

1 Answer 1

2

You can subtract last value by column GesamteMessung_Sec and add to_frame for convert Series to DataFrame:

df = pd.DataFrame({'GesamteMessung_Sec':[10,2,1,5]})
print (df)
   GesamteMessung_Sec
0                  10
1                   2
2                   1
3                   5

time_ges = (df.GesamteMessung_Sec.iloc[-1] - df.GesamteMessung_Sec).to_frame('echte_Ladezeit')
print (time_ges )
   echte_Ladezeit
0              -5
1               3
2               4
3               0

If need new column of original DataFrame:

df = pd.DataFrame({'GesamteMessung_Sec':[10,2,1,5]})
df['echte_Ladezeit'] = df.GesamteMessung_Sec.iloc[-1] - df.GesamteMessung_Sec
print (df)
   GesamteMessung_Sec  echte_Ladezeit
0                  10              -5
1                   2               3
2                   1               4
3                   5               0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.