Last answered:

14 Feb 2023

Posted on:

06 Feb 2023

0

Resolved: Is it Necessary to loop in Pandas DataFrames ?

in 4:17 min in this video (cell# 30), why did you use for loop in this code?  

sales_data['offsetUTC'] = [list_mos_timestamp[0].utcoffset().total_seconds()/3600 for i in range(len(sales_data))]

my understanding is, one of the big advantages of Pandas is the DataFrame brodcasing operations. i.e no need for looping in DataFrames

sales_data['offsetUTC'] = list_mos_timestamp[0].utcoffset().total_seconds()/3600

1 answers ( 1 marked as helpful)
Instructor
Posted on:

14 Feb 2023

0

Hi Yahia!


Thanks for reaching out.


Should we only refer to the first value in list_mos_timestamp (which is list_mos_timestamp[0]), then your remark is spot on. Indeed, that's the expression we use to obtain a single value (-0.5): 

list_mos_timestamp[0].utcoffset().total_seconds()/3600

Then, since we want to apply the .utcoffset() method to all values from list_mos_timestamp, we use the iterator variable i in the expression, instead of the first value only:

sales_data['offsetUTC'] = [list_mos_timestamp[i].utcoffset().total_seconds()/3600 for i in range(len(sales_data))]


Hope this helps.
Best,
Martin

Submit an answer