data = {'Date': ['2022-01-02', '2022-01-13','2022-02-12','2022-02-15']}
df = pd.DataFrame(data)
我創建了資料框,然后檢查新條目的 dtype 是什么
df['Date']
dtype 顯示為 dtype: datetime64[ns] 所以現在我嘗試創建虛擬變數來確定兩組日期之間是否存在某些內容:
df['2/11-2/13 DV']=df['Date'].apply(lambda x: 1 if (2022-02-13 <= x <= 2022-02-13) else 0)
但它始終回傳相同的錯誤: TypeError: '<=' not supported between 'int' and 'Timestamp' 任何有關如何解決此錯誤的幫助將不勝感激。
uj5u.com熱心網友回復:
嘗試:
df["2/11-2/13 DV"] = (
(df["Date"] >= "2022-02-11") & (df["Date"] <= "2022-02-23")
).astype(int)
print(df)
印刷:
Date 2/11-2/13 DV
0 2022-01-02 0
1 2022-01-13 0
2 2022-02-12 1
3 2022-02-15 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/516244.html
標籤:Python熊猫约会时间
上一篇:如何將字串轉換為日期時間格式?
