我有一個如下所示的表:
-------- -------- ----------- ------------
| Binary | Status | Inception | New Status |
-------- -------- ----------- ------------
| 1 | Raising| 2016 | |
| 1 | Raising| 2017 | |
| 0 | Failed | 2018 | |
| 1 | Closed | 2021 | |
| 1 | Raising| 2020 | |
我一直在填充New Status列如下:
q = df.query('Binary == 1 and Inception <= 2022 - 3 and Status != "Closed" and Status != "Failed"')
my_query_index = q.index
my_query_index
df.iloc[my_query_index, 3] = "Closed"
df
但是,我想以更動態的方式執行此操作,<在第一種情況下使用 a ,<=在第二種情況下使用 a 。在這種情況下,如果今天的日期恰好在上半年,我將使用查詢
q = df.query('Binary == 1 and Inception < 2022 - 3 and Status != "Closed" and Status != "Failed"')
my_query_index = q.index
my_query_index
df.iloc[my_query_index, 3] = "Closed"
df
如果今天的日期恰好在下半年,請使用
q = df.query('Binary == 1 and Inception <= 2022 - 3 and Status != "Closed" and Status != "Failed"')
my_query_index = q.index
my_query_index
df.iloc[my_query_index, 3] = "Closed"
df
鑒于 ,我想用 替換Inception(在代碼中)。我不確定如何將這兩種情況合并到代碼中,也不知道如何將其合并到查詢中。todaytoday = datetime.today().strftime('%Y-%m-%d')
uj5u.com熱心網友回復:
您可以使用 str 格式進行查詢
today = datetime.today().strftime('%Y-%m-%d')
half_year = '2022-06-01'
if today < half_year:
query = 'Binary == 1 and Inception < 2022 - 3 and Status != "Closed" and Status != "Failed"'
else:
query = 'Binary == 1 and Inception <= 2022 - 3 and Status != "Closed" and Status != "Failed"'
然后你的常規代碼
q = df.query(query)
my_query_index = q.index
my_query_index
df.iloc[my_query_index, 3] = "Closed"
df
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451578.html
上一篇:類方法中的全域私有函式使用
