您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Python怎么實現Excel文件的合并”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Python怎么實現Excel文件的合并”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
將2020下的所有文件進行合并,成一個文件:
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("所有數據文件總共有%s" %len(csv_list)) for i in csv_list: fr=open(i,"rb").read() #除了第一個數據文件外,其他不讀取表頭 with open('../output/covid19temp0314.csv','ab') as f: f.write(fr) f.close() print('數據合成完畢!')
合并后的數據:
## 02 使用函數進行數據合并 import os import pandas as pd # 定義函數(具有遞歸功能) 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("請輸入數據文件所在目錄:") 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年的數據還沒有,所以不合并
(* ̄(oo) ̄)注: 這個的等待時間應該會比較長,因為一共有一百九十多萬條數據。
將合并后的數據進行保存:
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之前的數據:
beforedf=pd.read_csv(r'D:\Python\03DataAcquisition\COVID-19\before20201111.csv',encoding='utf-8-sig')
beforedf.info()
將兩組數據合并:
tempalldf=beforedf.append(csvdatadf) tempalldf.head()
如圖所示:要將Country_Region從Hong Kong變成China。澳門和臺灣也是如此:
查找有關臺灣的數據:
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']
香港的數據處理:
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'
澳門的數據處理:
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'
最終將整理好的數據進行保存:
beforedf.to_csv("beforedf0314.csv",index=None,encoding='utf-8-sig') afterdf.to_csv("afterdf0314.csv",index=None,encoding='utf-8-sig')
讀到這里,這篇“Python怎么實現Excel文件的合并”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。