Python Forum
How to convert time to sec in boucle for
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert time to sec in boucle for
#1
Hello i'm new in data science,and i find a good csv data about tv news duration by type.
i ave first to convert time to seconds:
>>> x = time.strptime('00:05:00,000'.split(',')[0],'%H:%M:%S')
>>> datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()
300.0
but for all my data it's more difficult:
for x in pd.read_csv("/home/me/Documents/ina-barometre-jt-tv-donnees-mensuelles-2005-2020-durees.csv",skiprows=2, usecols="C:H",index_col=0) :
    x = time.strptime(x.split(',')[0],'%H:%M:%S')
    datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()

ValueError: 'usecols' must either be list-like of all strings, all unicode, all integers or a callable.
    
thanks for your help. Wall
Reply
#2
import pandas as pd 
import numpy as np 
import datetime
import time
Chronos=[]
duree =0
sumSec = 0


data = np.loadtxt("C:\\Users\\me\\Documents\\ina-barometre-jt-tv-donnees-mensuelles-2005-2020-durees.csv",skiprows=2,delimiter =';',
dtype=[('Date',np.object),('Theme',np.object),('TF1',np.object),('France_2',np.object),('France_3',np.object),('canal_+',np.object),
('Arte',np.object),('M6',np.object)])

declar = pd.DataFrame(data)

pre = declar ['M6']

for y in pre:
    x = time.strptime(y,'%H:%M:%S')
    duree=datetime.timedelta(hours=x.tm_hour, minutes=x.tm_min, seconds=x.tm_sec).total_seconds()
    Chronos.append(duree/3600)
    sumSec = sumSec + duree
    sumCH = sumSec/3600

#print('sumCH =',sumCH)  
pre2 = pd.Series(Chronos,declar['Theme'])
#print(pre2)

calc=pre2.groupby(['Theme']).sum()

print((calc/sumCH)*100)

calc.plot.pie()
OUTPUT:
[Image: mert.png]

Theme /pourcentage
Catastrophes 6.136841
Culture-loisirs 12.190127
Economie 8.240933
Education 2.635510
Environnement 3.274924
Faits divers 6.546009
Histoire-hommages 1.997193
International 8.648323
Justice 5.734435
Politique France 9.517616
Santé 6.844424
Sciences et techniques 2.222238
Société 19.354383
Sport 6.657045
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I convert time-series data in rows into column srvmig 0 2,064 Apr-11-2020, 05:40 AM
Last Post: srvmig
  pandas convert date/time to week okl 3 6,675 Mar-03-2018, 10:15 PM
Last Post: marsokod

Forum Jump:

User Panel Messages

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