Python實現(xiàn)Excel文件的合并(以新冠疫情數(shù)據(jù)為例)
注:本篇文章以新冠疫情數(shù)據(jù)文件的合并為例。
需要相關(guān)數(shù)據(jù)的請移步:》2020-2022年新冠疫情數(shù)據(jù)
一、單目錄下面的數(shù)據(jù)合并

將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("所有數(shù)據(jù)文件總共有%s" %len(csv_list))
for i in csv_list:
fr=open(i,"rb").read() #除了第一個數(shù)據(jù)文件外,其他不讀取表頭
with open('../output/covid19temp0314.csv','ab') as f:
f.write(fr)
f.close()
print('數(shù)據(jù)合成完畢!')

合并后的數(shù)據(jù):

二、使用函數(shù)進行數(shù)據(jù)合并
## 02 使用函數(shù)進行數(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("請輸入數(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) ̄)注: 這個的等待時間應(yīng)該會比較長,因為一共有一百九十多萬條數(shù)據(jù)。
將合并后的數(shù)據(jù)進行保存:
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()

三、處理港澳臺數(shù)據(jù)

如圖所示:要將Country_Region從Hong Kong變成China。澳門和臺灣也是如此:
查找有關(guān)臺灣的數(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ù)進行保存:
beforedf.to_csv("beforedf0314.csv",index=None,encoding='utf-8-sig')
afterdf.to_csv("afterdf0314.csv",index=None,encoding='utf-8-sig')

到此這篇關(guān)于Python實現(xiàn)Excel文件的合并(以新冠疫情數(shù)據(jù)為例)的文章就介紹到這了,更多相關(guān)Python合并Excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于sklearn實現(xiàn)Bagging算法(python)
這篇文章主要為大家詳細介紹了基于sklearn實現(xiàn)Bagging算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
Python實現(xiàn)Mysql數(shù)據(jù)統(tǒng)計及numpy統(tǒng)計函數(shù)
這篇文章主要介紹了Python實現(xiàn)Mysql數(shù)據(jù)統(tǒng)計的實例代碼,給大家介紹了Python數(shù)據(jù)分析numpy統(tǒng)計函數(shù)的相關(guān)知識,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
Python中使用Opencv開發(fā)停車位計數(shù)器功能
這篇文章主要介紹了Python中使用Opencv開發(fā)停車位計數(shù)器,本教程最好的一點就是我們將使用基本的圖像處理技術(shù)來解決這個問題,沒有使用機器學習、深度學習進行訓(xùn)練來識別,感興趣的朋友跟隨小編一起看看吧2022-04-04
解決Python報錯No module named Crypto問題
這篇文章主要介紹了解決Python報錯No module named“Crypto”問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
Python 代碼中的 yield 關(guān)鍵字到底是什么
yield是Python中一個強大的工具,它可以幫助你以一種高效的方式處理大量數(shù)據(jù),理解yield的工作原理對于掌握Python編程至關(guān)重要,這篇文章主要介紹了Python 代碼中的 yield 到底是什么,需要的朋友可以參考下2024-07-07

