從多個Excel文件中抓取數據並將它們合併到一個數據幀中。html
import pandas as pd import matplotlib import os import sys %matplotlib inline
print('Python version ' + sys.version) print('Pandas version ' + pd.__version__) print('Matplotlib version ' + matplotlib.__version__)
# Create DataFrame
d = {'Channel':[1], 'Number':[255]} df = pd.DataFrame(d) df
# Export to Excel
df.to_excel('test1.xlsx', sheet_name = 'test1', index = False) df.to_excel('test2.xlsx', sheet_name = 'test2', index = False) df.to_excel('test3.xlsx', sheet_name = 'test3', index = False) print('Done')
獲取文件名列表,確保文件夾中沒有其餘Excel文件。python
# List to hold file names
FileNames = [] # Your path will be different, please modify the path below. os.chdir(r"C:\Users\david\notebooks\update") # Find any file that ends with ".xlsx" for files in os.listdir("."): if files.endswith(".xlsx"): FileNames.append(files) FileNames
建立一個函數來處理全部的excel文件。app
def GetFile(fnombre): # Path to excel file # Your path will be different, please modify the path below. location = r'C:\Users\david\notebooks\update\\' + fnombre # Parse the excel file # 0 = first sheet df = pd.read_excel(location, 0) # Tag record to file name df['File'] = fnombre # Make the "File" column the index of the df return df.set_index(['File'])
i.e.
df_list = [df, df, df]函數
# Create a list of dataframes
df_list = [GetFile(fname) for fname in FileNames] df_list
# Combine all of the dataframes into one
big_df = pd.concat(df_list) big_df
big_df.dtypes
# Plot it!
big_df['Channel'].plot.bar();
This tutorial was rewrited by CDSspa