무회blog

pandas,지정폴더, 엑셀 한번에 읽기, 시트포함 본문

Python

pandas,지정폴더, 엑셀 한번에 읽기, 시트포함

최무회 2020. 7. 22. 17:48

 

# 첫번째 시트를 기준으로 전부 읽기 
import os
import pandas as pd
dir = 'data/'
filenames = os.listdir(dir)
index = 0
dfs = []
for name in filenames:
	print(index)
	dfs.append(pd.read_excel(os.path.join(dir,name)))
	index += 1 #为了查看合并到第几个表格了
df = pd.concat(dfs)
df.to_excel('data/total.xlsx',index = False)

 

# 지정폴더안 엑셀 한번에 읽기, 모든 시트 
import os
import pandas as pd
read_path = './../test_data/test_duoyidian/'
dir = read_path
filenames = os.listdir(dir)
filenames
index = 0
dfs = []

read_file = 'test_Newsdata-100.xlsx'
read_path = './../test_data/'

df_list = [pd.read_excel(read_path+read_file, sheet_name = x) for x in range(6)]
df_list = [df_list[x][['subMenu', 'newsFrom', 'title', 'content', 'yoyag']] for x in range(6)]
df_ = pd.concat(df_list, axis=0)

for name in filenames:
    print(index)
    df_list = [pd.read_excel(os.path.join(dir,name),sheet_name = x)for x in range(6)]
    df_list = [df_list[x][['subMenu', 'newsFrom', 'title', 'content', 'yoyag']] for x in range(6)]
    df_ = pd.concat(df_list, axis=0)
    allExcelFile = df_
#     allExcelFile = pd.read_excel(os.path.join(dir,name))
    dfs.append(allExcelFile)
    index += 1 #为了查看合并到第几个表格了
df = pd.concat(dfs)
print(len(df))
df.to_excel('./total_excel.xlsx')
Comments