Python issue - Data science - Help is needed - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python issue - Data science - Help is needed (/thread-34416.html) |
Python issue - Data science - Help is needed - yovel - Jul-29-2021 best_5 = sorted_swap[:4] for row in best_5: hour = row[1] hour = dt.datetime.strptime(hour, '%H') hour = hour.strftime('%H') average = row[0] print(type(average)) string = "{a} {c:.2f} average comments per post" for_final = string.format(a=average, c=hour) print(for_final)This is the full list Hi so in general i want to understand what is the issue in those lines of codes--------------------------------------------------------------------------- Thanks
RE: Python issue - Data science - Help is needed - buran - Jul-29-2021 The problem is that hour is string and you use f (i.e. float number). I guess you want to do "{c} {a:.2f} average comments per post" insteadalso, don't use string as name, it's built-in module.also, best_5 will hold only four elements, not 5finally, your code can be simplified for comments, hour in sorted_swap[:5]: # I guess you can simply use sorted instead of custom sort function print(f"{hour} {comments:.2f} average comments per post") RE: Python issue - Data science - Help is needed - yovel - Jul-29-2021 THANKS! This is the things which your mind is overwhelmed with the learning etc and it dont get it where is the issue which was in front my eyes regarding the :[4 i guess i thought on the [1:] so i thought like here in the 1 example it includes the 1 so for the 4 it will include as well so i guess its not the same So now its working good thanks bro! 15:00 38.59 average comments per post <class 'float'> 02:00 23.81 average comments per post <class 'float'> 20:00 21.52 average comments per post <class 'float'> 16:00 16.80 average comments per post <class 'float'> 21:00 16.01 average comments per post and in the self learning platform they didn't mentioned the f thing you did in the print (this is where i guess i need to understand the learning wont be perfect unless i will make some serious course online of data science or python etc.. Im new to this "Arena" almost a month the platform im using right now is Data quest which to be honest is good except sometime the explanation isn't the best but 80% of the time they are fine So i added the %M to the hour i didnt know i can add it even if it's not exist in the strptime format Anyway thank and i appreciate your explanation as well the fast response since im waiting a day to solve it and i wanted to continue with the learning |