欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)Excel文件的合并(以新冠疫情數(shù)據(jù)為例)

 更新時間:2022年03月19日 14:22:48   作者:渴望成為寂寞勝者  
這篇將以新冠疫情數(shù)據(jù)為例,詳細介紹了如何利用Python實現(xiàn)合并Excel文件,文中的示例代碼講解詳細,感興趣的可以了解一下

注:本篇文章以新冠疫情數(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)文章

最新評論