Python Forum

Full Version: Python issue - Data science - Help is needed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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

Output:
[[38.5948275862069, '15'], [23.810344827586206, '02'], [21.525, '20'], [16.796296296296298, '16'], [16.009174311926607, '21'], [14.741176470588234, '13'], [13.440677966101696, '10'], [13.24074074074074, '18'], [13.233644859813085, '14'], [11.46, '17'], [11.383333333333333, '01'], [11.051724137931034, '11'], [10.8, '19'], [10.25, '08'], [10.08695652173913, '05'], [9.41095890410959, '12'], [9.022727272727273, '06'], [8.12962962962963, '00'], [7.985294117647059, '23'], [7.852941176470588, '07'], [7.796296296296297, '03'], [7.170212765957447, '04'], [6.746478873239437, '22'], [5.5777777777777775, '09']]
Hi so in general i want to understand what is the issue in those lines of codes
---------------------------------------------------------------------------
Error:
ValueError Traceback (most recent call last) <ipython-input-8-9994e6baa044> in <module> 10 print(type(average)) 11 string = "{a} {c:.2f} average comments per post" ---> 12 for_final = string.format(a=average, c=hour) 13 print(for_final) ValueError: Unknown format code 'f' for object of type 'str'
Thanks
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" instead

also, don't use string as name, it's built-in module.
also, best_5 will hold only four elements, not 5
finally, 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")
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