Python Forum
Python issue - Data science - Help is needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python issue - Data science - Help is needed
#1
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
buran write Jul-29-2021, 02:37 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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")
yovel likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data Science sadra_ss 1 333 Oct-14-2021, 07:01 PM
Last Post: buran
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 746 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Databricks, Python Notebook Data file use issue khalid2200 0 800 Nov-25-2020, 03:36 AM
Last Post: khalid2200
  Help needed for a python package keysson 1 1,063 Sep-02-2020, 03:37 AM
Last Post: Larz60+
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 1,085 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  Absolutely new to python - basic advise needed mariolucas75 2 994 Jun-12-2020, 08:36 PM
Last Post: Yoriz
  Issue with creating an array of pixel data for PNG files in Google Colab The_Sarco 1 901 Apr-29-2020, 12:03 AM
Last Post: bowlofred
Smile Help needed. Python Newbie!, it will be fun. knightdea 3 1,205 Oct-13-2019, 08:50 AM
Last Post: perfringo
  Issue while inserting data into Teradata via python dataframe sandy 2 2,731 Feb-10-2019, 08:06 PM
Last Post: sandy
  help needed with python launcher fallenlight 3 2,041 Jan-19-2019, 01:06 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020