我有一個問題,一個有點復雜的問題。
我有一個這樣的資料框:
commodity_name first_delivery_date last_delivery_date last_trading_date tenor delivery_window new_tenor Vol
<chr> <dttm> <dttm> <dttm> <chr> <chr> <chr> <int>
1 oil 2021-06-01 00:00:00 2021-06-30 00:00:00 2021-04-30 00:00:00 month Jun 21 Jun 21 29000
2 gold 2022-03-01 00:00:00 2022-03-31 00:00:00 2022-02-28 00:00:00 month Mar 22 Mar 22 -800
3 oil 2021-07-01 00:00:00 2021-07-31 00:00:00 2021-05-31 00:00:00 month Jul 21 Jul 21 -21000
4 gold 2021-09-01 00:00:00 2021-09-30 00:00:00 2021-08-31 00:00:00 month Sep 21 Sep 21 1100
5 gold 2021-02-01 00:00:00 2021-02-28 00:00:00 2021-01-29 00:00:00 month Feb 21 Feb 21 -3000
6 depower 2021-01-01 00:00:00 2021-01-31 00:00:00 2020-12-30 00:00:00 quarter Jan 21 Q1 21 -3
7 oil 2022-04-01 00:00:00 2022-04-30 00:00:00 2022-02-28 00:00:00 month Apr 22 Apr 22 23000
8 czpower 2023-02-01 00:00:00 2023-02-28 00:00:00 2023-01-30 00:00:00 quarter Feb 23 Q1 23 26
9 oil 2021-02-01 00:00:00 2021-02-28 00:00:00 2020-12-31 00:00:00 quarter Feb 21 Q1 21 -17000
10 gold 2021-05-01 00:00:00 2021-05-31 00:00:00 2021-04-30 00:00:00 month May 21 May 21 2400
我想從中創建另一個資料框,基于以下條件:
- 對于 Year
YY,如果new_tenor是Q1 YY在舊資料框中:在新資料框中創建三行,分別new_tenor是和。所有其他變數保持不變;Jan YYFeb YYMar YY - 如果
new_tenor是Q2 YY在舊資料框中:在新資料框中創建三行,分別new_tenor是Apr YY,May YY和。Jun YY所有其他變數保持不變; - 如果
new_tenor是Q3 YY在舊資料框中:在新資料框中創建三行,分別new_tenor是Jul YY,Aug YY和。Sep YY所有其他變數保持不變; - 如果
new_tenor是Q4 YY在舊資料框中:在新資料框中創建三行,分別new_tenor是Oct YY,Nov YY和。Dec YY所有其他變數保持不變; - 如果
new_tenor在Cal YY舊資料框中:在新資料框中創建六行,其中分別new_tenor是Jan YY 1、Feb YY 1、Mar YY 1、和。所有其他變數保持不變;Q2 YY 1Q3 YY 1Q4 YY 1
問題很簡單,主要取決于 的值YY,新資料框中的其他所有內容都與舊資料框中的相同。
我嘗試使用以下代碼解決問題:
my_df = []
for index, row in ss.iterrows():
# d = row["NewTenor"].split()
# year = d[1]
print(year)
if "Cal" in row["NewTenor"]:
# Go to next year
# Add Jan, Feb, and Mar
temp_1 = row
temp_1['NewTenor'] = temp_1['NewTenor'].replace({'Cal':'Jan','21':'22','22':'23','23':'24'})
temp_2 = row
temp_2['NewTenor'] = temp_2['NewTenor'].replace({'Cal':'Feb','21':'22','22':'23','23':'24'})
temp_3 = row
temp_3['NewTenor'] = temp_3['NewTenor'].replace({'Cal':'Mar','21':'22','22':'23','23':'24'})
# Add Q2, Q3, and Q4
temp_4 = row
temp_4['NewTenor'] = temp_1['NewTenor'].replace({'Cal':'Q2','21':'22','22':'23','23':'24'})
temp_5 = row
temp_5['NewTenor'] = temp_1['NewTenor'].replace({'Cal':'Q3','21':'22','22':'23','23':'24'})
temp_6 = row
temp_6['NewTenor'] = temp_1['NewTenor'].replace({'Cal':'Q4','21':'22','22':'23','23':'24'})
# Append to data frame
my_df.append(temp_1)
my_df.append(temp_2)
my_df.append(temp_3)
my_df.append(temp_4)
my_df.append(temp_5)
my_df.append(temp_6)
elif "Q1" in row["NewTenor"]:
# Add Jan, Feb, and Mar
temp_1 = row
temp_1['NewTenor'] = temp_1['NewTenor'].replace({'Q1':'Jan'})
temp_2 = row
temp_2['NewTenor'] = temp_2['NewTenor'].replace({'Q1':'Feb'})
temp_3 = row
temp_3['NewTenor'] = temp_3['NewTenor'].replace({'Q1':'Mar'})
# Append to data frame
my_df.append(temp_1)
my_df.append(temp_2)
my_df.append(temp_3)
elif "Q2" in row["NewTenor"]:
# Add Apr, May, and Jun
temp_1 = row
temp_1['NewTenor'] = temp_1['NewTenor'].replace({'Q2':'Apr'})
temp_2 = row
temp_2['NewTenor'] = temp_2['NewTenor'].replace({'Q2':'May'})
temp_3 = row
temp_3['NewTenor'] = temp_3['NewTenor'].replace({'Q2':'Jun'})
# Append to data frame
my_df.append(temp_1)
my_df.append(temp_2)
my_df.append(temp_3)
elif "Q3" in row["NewTenor"]:
# Add Jul, Aug, and Sep
temp_1 = row
temp_1['NewTenor'] = temp_1['NewTenor'].replace({'Q3':'Jul'})
temp_2 = row
temp_2['NewTenor'] = temp_2['NewTenor'].replace({'Q3':'Aug'})
temp_3 = row
temp_3['NewTenor'] = temp_3['NewTenor'].replace({'Q3':'Sep'})
# Append to data frame
my_df.append(temp_1)
my_df.append(temp_2)
my_df.append(temp_3)
else :
# Add Oct, Nov, and Dec
temp_1 = row
temp_1['NewTenor'] = temp_1['NewTenor'].replace({'Q4':'Oct'})
temp_2 = row
temp_2['NewTenor'] = temp_2['NewTenor'].replace({'Q4':'Nov'})
temp_3 = row
temp_3['NewTenor'] = temp_3['NewTenor'].replace({'Q4':'Dec'})
# Append to data frame
my_df.append(temp_1)
my_df.append(temp_2)
my_df.append(temp_3)
my_df = pd.DataFrame(my_df)
這并不復雜,它總是給我錯誤。
有人可以幫我創建新的資料框嗎?先感謝您。
uj5u.com熱心網友回復:
如果我理解正確:
def split_tenor(tenor):
start, year = tenor.split(" ")
if start == "Cal":
months = ["Jan", "Feb", "Mar", "Q2", "Q3", "Q4"]
year = int(year) 1
elif start == "Q1":
months = ["Jan", "Feb", "Mar"]
elif start == "Q2":
months = ["Apr", "May", "Jun"]
elif start == "Q3":
months = ["Jul", "Aug", "Sep"]
elif start == "Q4":
months = ["Oct", "Nov", "Dec"]
else:
return tenor
return [f"{m} {year}" for m in months]
df["new_tenor"] = df["new_tenor"].apply(split_tenor)
df.explode("new_tenor")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/481370.html
