我有一個從軟體生成的結果檔案,它看起來像這樣:
,0,1,2,3,4,5,6,7,8,9
0,Month,Decade,Stage,Kc,ETc,ETc,Eff,rain,Irr.,Req.
1,coeff,mm/day,mm/dec,mm/dec,mm/dec,,,,,
2,Sep,1,Init,0.50,1.85,18.5,21.8,0.0,,
3,Sep,2,Init,0.50,1.77,17.7,30.3,0.0,,
4,Sep,3,Init,0.50,1.72,17.2,37.1,0.0,,
5,Oct,1,Deve,0.61,2.05,20.5,49.5,0.0,,
6,Oct,2,Deve,0.82,2.66,26.6,59.3,0.0,,
7,Oct,3,Deve,1.03,3.24,35.6,43.0,0.0,,
8,Nov,1,Mid,1.20,3.63,36.3,20.9,15.4,,
9,Nov,2,Mid,1.21,3.53,35.3,6.0,29.2,,
10,Nov,3,Mid,1.21,3.70,37.0,4.0,33.0,,
11,Dec,1,Mid,1.21,3.87,38.7,0.1,38.6,,
12,Dec,2,Late,1.18,3.92,39.2,0.0,39.2,,
13,Dec,3,Late,1.00,3.58,39.4,0.0,39.4,,
14,Jan,1,Late,0.88,3.36,10.1,0.0,10.1,,
15,,,,,,,,,,
16,372.1,272.2,204.9,,,,,,,
正如人們所觀察到的,月份從九月到一月不等。每個月分為三個部分或十年。確切地說,月份從 2017 年 9 月到 2018 年 1 月的第 1 個十年不等。現在,我必須以這種格式生成具有一個月中每個十年的開始日期的日期:01-Sep-2017。所以我將有 2017 年 9 月 1 日、2017 年 9 月 11 日、2017 年 9 月 21 日、...、2018 年 1 月 1 日。如何生成這些日期?我將分享我到目前為止撰寫的代碼。
years = [2017, 2018, 2019]
temp = pd.read_csv(folder_link) # Reading the particular result file
Month = temp['0'][2:] # First column = Month (Jul, Aug, ..)
Decade = temp['1'][2:]
for year in years:
for j in range(2,len(Decade)): # First two lines are headers, so removed them
if(int(Decade[j]) == 1): # First decade = 1-10 days of month
Date = "1" "-" Month[j] "-" str(year) # Writing the date as 1-Jan-2017
Dates.append(Date)
if(int(Decade[j]) == 2): # Second decade = 11-20 days of month
Date = "11" "-" Month[j] "-" str(year)
Dates.append(Date)
if(int(Decade[j]) == 3): # Third decade = 21-28 or 21-30 or 21-31 days of month
Date = "21" "-" Month[j] "-" str(year)
Dates.append(Date)
此代碼的問題是我將得到 01-Sep-2017、11-Sep-2017、21-Sep-2017、...、01-Jan- 2017(而不是 2018)。我需要一個適用于所有月份的通用解決方案,而不僅僅是一月份。我有一些從 2017 年 9 月到 2018 年 8 月的結果。有什么幫助嗎?
uj5u.com熱心網友回復:
如果您想繼續使用迭代方法(使用 Pandas 函式可能更有效),這是一種簡單的方法:
dates = []
year = 2017
month_list = ['Jan', 'Sep', 'Oct', 'Nov', 'Dec']
temp = pd.read_csv("data.csv") # Reading the particular result file
for index, row in temp.iterrows():
# First two lines are headers, so skip them. Same for last two lines.
if index > 1 and row[1] in month_list:
if row[1] == 'Jan':
year = 1
if(int(row[2]) == 1): # First decade = 1-10 days of month
date = "1" "-" row[1] "-" str(year) # Writing the date as 1-Jan-2017
dates.append(date)
elif(int(row[2]) == 2): # Second decade = 11-20 days of month
date = "11" "-" row[1] "-" str(year)
dates.append(date)
elif(int(row[2]) == 3): # Third decade = 21-28 or 21-30 or 21-31 days of month
date = "21" "-" row[1] "-" str(year)
dates.append(date)
else:
print("Unrecognized value for month {}".format(row[2]))
pass
print(dates)
解釋 :
- 用于
iterrows迭代您的資料框行 - 然后,跳過標題并通過查看月份值(使用預定義串列)來檢查您是否正在決議實際資料
- 最后,當您的月份值為
Jan
*注意:此解決方案假定您的資料是按時間順序排列行的時間序列。
PS:Python 中的類只使用大寫字母,而不是變數。
uj5u.com熱心網友回復:
首先,您可以在讀取 csv 檔案時正確設定列和索引。然后你可以用一個公式從十年推匯出一天。
僅從 12 月切換到 1 月時遞增年份(如果缺少 1 月和/或 12 月,您可以在此處擴展您的條件)。
應用這些代碼后,代碼將變得更容易閱讀和理解:
temp = pd.read_csv(folder_link, header=1, index_col=0)
Dates = []
year = 2017
for index, row in temp.iloc[1:].iterrows():
month = row["Month"]
if month == "Jan" and temp.at[index-1, "Month"] == "Dec":
year = 1 # incrementing year if row is january while preceding row is december
day = (int(row["Decade"]) - 1) * 10 1
Dates.append(f"{day}-{month}-{year}")
print(Dates)
輸出:
['1-Sep-2017', '11-Sep-2017', '21-Sep-2017', '1-Oct-2017', '11-Oct-2017', '21-Oct-2017', '1-Nov-2017', '11-Nov-2017', '21-Nov-2017', '1-Dec-2017', '11-Dec-2017', '21-Dec-2017', '1-Jan-2018']
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/348059.html
上一篇:按日期對多個字串進行排序
下一篇:回傳每個日歷月的作業日數
