這篇文章給大家介紹利用pandas怎么對數(shù)據(jù)進(jìn)行導(dǎo)出,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

具體方法如下:
import pandas as pd
import pymysql
df = pd.DataFrame({'A': [3, 4, 8, 9], 'B': [1.2, 2.4, 4.5, 7.3], 'C': ["aa", "bb", "cc", "dd"]})
def export_data_to_csv():
# 參數(shù)encoding="utf_8_sig"編碼后,可以防止寫入csv的中文出現(xiàn)亂碼
df.to_csv("./test.csv", encoding="utf_8_sig")
def export_data_to_excel():
# encoding編碼方式,sheet_name表示要寫到的sheet名稱, 默認(rèn)為0, header=None表示不含列名
df.to_excel("./test.xlsx", encoding="utf_8_sig", sheet_name=0, header=None)
def export_data_to_table():
con = pymysql.connect(host="127.0.0.1", user="username", password="password", database="dbname", charset='utf8',
use_unicode=True)
df.to_sql(name='table_name', con=con, if_exists='append', index=False)
def export_data_to_json():
df.to_json("test.txt")
def main():
export_data_to_csv() # 導(dǎo)出數(shù)據(jù)到CSV文件
export_data_to_excel() # 導(dǎo)出數(shù)據(jù)到Excel文件
export_data_to_table() # 導(dǎo)出數(shù)據(jù)到SQL表
export_data_to_json() # 以Json格式導(dǎo)出數(shù)據(jù)到文本文件
if __name__ == '__main__':
main()關(guān)于利用pandas怎么對數(shù)據(jù)進(jìn)行導(dǎo)出就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁名稱:利用pandas怎么對數(shù)據(jù)進(jìn)行導(dǎo)出-創(chuàng)新互聯(lián)
URL分享:http://www.chinadenli.net/article20/dcphco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站營銷、Google、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容