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)文章
Python數(shù)據(jù)可視化圖實現(xiàn)過程詳解
這篇文章主要介紹了Python數(shù)據(jù)可視化圖實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06在spyder IPython console中,運行代碼加入?yún)?shù)的實例
這篇文章主要介紹了在spyder IPython console中,運行代碼加入?yún)?shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python創(chuàng)建簡單的神經(jīng)網(wǎng)絡(luò)實例講解
在本篇文章里小編給大家整理的是一篇關(guān)于如何在Python中創(chuàng)建一個簡單的神經(jīng)網(wǎng)絡(luò)的相關(guān)知識點,有興趣的朋友們可以參考下。2021-01-01Python爬蟲實例——scrapy框架爬取拉勾網(wǎng)招聘信息
這篇文章主要介紹了Python爬蟲實例——scrapy框架爬取拉勾網(wǎng)招聘信息的相關(guān)資料,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07