我在 python 中有一個 SQL 查詢,它在時間戳上過濾,但是時間戳根據資料幀中的值而變化。
query = """(SELECT [ID],[Timestamp],[Value] FROM [table] Where [Timestamp] >= '2021-10-13') alias"""
big_df = spark.read.format("jdbc").option("driver", driver).option("url", url).option("dbtable", query).load()
如何將下面的“2021-10-13”替換為值將更改的python資料幀中插入的值?例如,在此資料框中將是 2021-01-01。
print(somedf.Timestamp.min())
'2021-01-01'
query = """(SELECT [ID],[Timestamp],[Value] FROM [table] Where [Timestamp] >= somedf.Timestamp.min()) alias"""
顯然頂線不起作用
uj5u.com熱心網友回復:
在我看來,它非常適合字串格式化:
query = f"(SELECT [ID],[Timestamp],[Value] FROM [table] Where [Timestamp] >= {somedf.Timestamp.min()} alias)"
不要忘記f字串開頭的小寫字母,表示它已格式化。讓我知道這是否對您不起作用。
大衛
uj5u.com熱心網友回復:
你可以像這樣插入一些字串
query = """(SELECT [ID],[Timestamp],[Value] FROM [table] Where [Timestamp] >= %s) alias""" % somedf.Timestamp.min().strftime("%Y-%d-%m")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/325102.html
上一篇:按條件將一列拆分為幾列
