您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Python怎么實(shí)現(xiàn)Excel文件的合并”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Python怎么實(shí)現(xiàn)Excel文件的合并”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
將2020下的所有文件進(jìn)行合并,成一個(gè)文件:
import requests import json import openpyxl import datetime import datetime as dt import time import pandas as pd import csv from openpyxl import load_workbook from sqlalchemy import create_engine import math import os import glob
csv_list=glob.glob(r'D:\Python\03DataAcquisition\COVID-19\2020\*.csv') print("所有數(shù)據(jù)文件總共有%s" %len(csv_list)) for i in csv_list: fr=open(i,"rb").read() #除了第一個(gè)數(shù)據(jù)文件外,其他不讀取表頭 with open('../output/covid19temp0314.csv','ab') as f: f.write(fr) f.close() print('數(shù)據(jù)合成完畢!')
合并后的數(shù)據(jù):
## 02 使用函數(shù)進(jìn)行數(shù)據(jù)合并 import os import pandas as pd # 定義函數(shù)(具有遞歸功能) def mergeFile(parent,path="",pathdeep=0,filelist=[],csvdatadf=pd.DataFrame(),csvdata=pd.DataFrame()): fileAbsPath=os.path.join(parent,path) if os.path.isdir(fileAbsPath)==True: if(pathdeep!=0 and ('.ipynb_checkpoints' not in str(fileAbsPath))): # =0代表沒有下一層目錄 print('--'+path) for filename2 in os.listdir(fileAbsPath): mergeFile(fileAbsPath,filename2,pathdeep=pathdeep+1) else: if(pathdeep==2 and path.endswith(".csv") and os.path.getsize(parent+'/'+path)>0): filelist.append(parent+'/'+path) return filelist # D:\Python\03DataAcquisition\COVID-19 path=input("請(qǐng)輸入數(shù)據(jù)文件所在目錄:") filelist=mergeFile(path) filelist csvdata=pd.DataFrame() csvdatadf=pd.DataFrame() for m in filelist: csvdata=pd.read_csv(m,encoding='utf-8-sig') csvdatadf=csvdatadf.append(csvdata) # 由于2023年的數(shù)據(jù)還沒有,所以不合并
(* ̄(oo) ̄)注: 這個(gè)的等待時(shí)間應(yīng)該會(huì)比較長(zhǎng),因?yàn)橐还灿幸话倬攀嗳f(wàn)條數(shù)據(jù)。
將合并后的數(shù)據(jù)進(jìn)行保存:
csvdatadf.to_csv("covid190314.csv",index=None,encoding='utf-8-sig')
csvdatadf=pd.read_csv("covid190314.csv",encoding='utf-8-sig') csvdatadf.info()
讀取新冠疫情在2020/0101之前的數(shù)據(jù):
beforedf=pd.read_csv(r'D:\Python\03DataAcquisition\COVID-19\before20201111.csv',encoding='utf-8-sig')
beforedf.info()
將兩組數(shù)據(jù)合并:
tempalldf=beforedf.append(csvdatadf) tempalldf.head()
如圖所示:要將Country_Region從Hong Kong變成China。澳門和臺(tái)灣也是如此:
查找有關(guān)臺(tái)灣的數(shù)據(jù):
beforedf.loc[beforedf['Country/Region']=='Taiwan'] beforedf.loc[beforedf['Country/Region'].str.contains('Taiwan')]
beforedf.loc[beforedf['Country/Region'].str.contains('Taiwan'),'Province/State']='Taiwan' beforedf.loc[beforedf['Province/State']=='Taiwan','Country/Region']='China' beforedf.loc[beforedf['Province/State']=='Taiwan']
香港的數(shù)據(jù)處理:
beforedf.loc[beforedf['Country/Region'].str.contains('Hong Kong'),'Province/State']='Hong Kong' beforedf.loc[beforedf['Province/State']=='Hong Kong','Country/Region']='China' afterdf.loc[afterdf['Country_Region'].str.contains('Hong Kong'),'Province_State']='Hong Kong' afterdf.loc[afterdf['Province_State']=='Hong Kong','Country_Region']='China'
澳門的數(shù)據(jù)處理:
beforedf.loc[beforedf['Country/Region'].str.contains('Macau'),'Province/State']='Macau' beforedf.loc[beforedf['Province/State']=='Macau','Country/Region']='China' afterdf.loc[afterdf['Country_Region'].str.contains('Macau'),'Province_State']='Macau' afterdf.loc[afterdf['Province_State']=='Macau','Country_Region']='China'
最終將整理好的數(shù)據(jù)進(jìn)行保存:
beforedf.to_csv("beforedf0314.csv",index=None,encoding='utf-8-sig') afterdf.to_csv("afterdf0314.csv",index=None,encoding='utf-8-sig')
讀到這里,這篇“Python怎么實(shí)現(xiàn)Excel文件的合并”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。