1 #在pandas中如果一列出現nan,則會認為他是float型別 2 import pandas 3 from datetime import datetime,timedelta 4 5 #關于月份加一的函式 6 def add_month(d,md): 7 yd=md//12 8 m=d.month+md%12 9 if m!=12: 10 yd=yd+m//12 11 m=m%12 12 return datetime(d.year+yd,m,d.day) 13 14 #header和sikprows作用一樣 15 excel=pandas.read_excel('填充資料.xlsx',header=3,usecols='C:F',index_col=None,dtype={'id':str,'store':str,'data':str}) 16 start=datetime(2020,2,10) 17 18 for i in excel.index: 19 excel.at[i,'id']=i+1 20 excel['store'].at[i]='Yes' if i%2==0 else 'No' 21 #excel['data'].at[i]=start + timedelta(days=i) #加一天,最多只能加到天 22 #excel['data'].at[i]=datetime(start.year+i,start.month,start.day) #每一年加一 23 excel['data'].at[i]=add_month(start,i) #對每個月加一 24 excel.set_index('id',inplace=True) 25 print(excel) 26 27 28 timedelta函式: 29 >>>from datetime import timedelta 30 >>>time1=timedelta(seconds=23,minutes=35,hours=13,days=6) 31 >>>time2=timedelta(seconds=59,minutes=57,hours=18,days=8) 32 >>>time2-time1 33 datetime.timedelta(days=2, seconds=19356) #回傳時間差
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/138778.html
標籤:Python
上一篇:向excel中寫入行,列
下一篇:繪制五星紅旗
