2017-01-20 122 views
1

我有日期時間(指數)的數據幀和用電量的採樣:熊貓(蟒蛇) - 段數據到時限

DateTime   Usage 
01-Jan-17 12am 10 
01-Jan-17 3am  5 
01-Jan-17 6am  15 
01-Jan-17 9am  40 
01-Jan-17 12pm 60 
01-Jan-17 3pm  62 
01-Jan-17 6pm  45 
01-Jan-17 9pm  18 
02-Jan-17 12am 11 
02-Jan-17 3am  4 
02-Jan-17 6am  17 
02-Jan-17 9am  37 
02-Jan-17 12pm 64 
02-Jan-17 3pm  68 
02-Jan-17 6pm  41 
02-Jan-17 9pm  16 

在現實中,這個系列是更長的時間。我試圖比較日常時間段,以便我可以查看時間序列的每日季節性。熊貓有沒有辦法分割數據,以便比較這些時間序列?我想象得到的DataFrame看起來像這樣:

Time 1-Jan 2-Jan 
12am 10  11 
3am  5  4 
6am  15  17 
9am  40  37 
12pm 60  64 
3pm  62  68 
6pm  45  41 
9pm  18  16 

謝謝!

回答

1

假設你有DateTime作爲str數據類型,您可以將其分成DateTime,然後轉動它:

df[['Date', 'Time']] = df.DateTime.str.split(" ", expand=True) 
df1 = df.pivot("Time", "Date", "Usage").reset_index() 

enter image description here


如何將Time列進行排序?實際上並不是那麼直截了當,要做到這一點,我們需要從時間,小時,PM/AM指標中提取一些列,並且如果小時數爲12,則應將12置於所有其他小時數之上:

# use regex to extract Hour (numeric part of Time) and AM/PM indicator 
hourInd = df1.Time.str.extract("(?P<Hour>\d+)(?P<Ind>[pa]m)", expand=True) 

# convert the hour column to integer and create another column to check if hour is 12 
# then sort by AM/PM indicator, IsTwelve and Hour and get the index to reorder the original 
# data frame 
df1.loc[(hourInd.assign(Hour = hourInd.Hour.astype(int), IsTwelve = hourInd.Hour != "12") 
     .sort_values(["Ind", "IsTwelve", "Hour"]).index)] 

enter image description here