實(shí)現(xiàn)功能
格式化字符串的兩種方法
實(shí)現(xiàn)代碼
# 方法一:format方法
query = '''
SELECT customer_id, COUNT(*) as num_orders
FROM orders
WHERE date >= '{start_date}' AND date <= '{end_date}'
GROUP BY customer_id
HAVING num_orders > {min_orders}
'''
start_date=1
end_date=2
min_orders=3
formatted_query = query.format(start_date=start_date, end_date=end_date, min_orders=min_orders)
print (formatted_query)
# 方法二:%方法
query = '''
SELECT salesperson, product, COUNT(*) AS num_sales
FROM sales
WHERE salesperson = %(salesperson)s
GROUP BY salesperson, product
HAVING num_sales >= %(min_sales)s
ORDER BY num_sales DESC;
'''
salesperson='a'
min_sales=10
formatted_query =query%{'salesperson': salesperson, 'min_sales': min_sales}
print (formatted_query)
實(shí)現(xiàn)效果
本人讀研期間發(fā)表5篇SCI數(shù)據(jù)挖掘相關(guān)論文,現(xiàn)在某研究院從事數(shù)據(jù)挖掘相關(guān)科研工作,對數(shù)據(jù)挖掘有一定認(rèn)知和理解,會(huì)結(jié)合自身科研實(shí)踐經(jīng)歷不定期分享關(guān)于python機(jī)器學(xué)習(xí)、深度學(xué)習(xí)、數(shù)據(jù)挖掘基礎(chǔ)知識與案例。
致力于只做原創(chuàng),以最簡單的方式理解和學(xué)習(xí),關(guān)注我一起交流成長。文章來源:http://www.zghlxwxcb.cn/news/detail-635838.html
1、邀請三個(gè)朋友關(guān)注V訂閱號:數(shù)據(jù)雜壇,即可在后臺(tái)聯(lián)系我獲取相關(guān)數(shù)據(jù)集和源碼,送有關(guān)數(shù)據(jù)分析、數(shù)據(jù)挖掘、機(jī)器學(xué)習(xí)、深度學(xué)習(xí)相關(guān)的電子書籍。?文章來源地址http://www.zghlxwxcb.cn/news/detail-635838.html
到了這里,關(guān)于Python數(shù)據(jù)分析實(shí)戰(zhàn)-格式化字符串的兩種方法(附源碼和實(shí)現(xiàn)效果)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!