Original English: 11-lesson
Reads data from multiple Excel files and merges the data together in a dataframe.
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 ' + Mat PLOTLIB.__VERSION__)
Python version 3.6.1 | Packaged by Conda-forge | (Default, Mar 2017, 21:57:00)
[GCC 4.2.1 compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Pandas version 0.19.2
matplotlib version 2.0.2
Create 3 Excel files
# Create Dataframe
d = {' Channel ': [1], ' number ': [255]}
df = pd. Dataframe (d)
DF
# Export to Excel file
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 ')
Done
Read 3 Excel file data into a dataframe
Read the Excel file name into a list and make sure that there are no other Excel files in the directory.
# Place the name of the list
FileNames = []
# Your path to the Excel file may be different and needs to be modified.
Os.chdir (r "./")
# Find all file name extensions are. xlsx files for file in
os.listdir ("."):
if Files.endswith (". xlsx"):
filenames.append (Files)
FileNames
[' test1.xlsx ', ' test2.xlsx ', ' test3.xlsx ']
Create a function to process all the Excel files.
def GetFile (Fnombre):
# Path of Excel file
# Your path to the Excel file may be different and needs to be modified.
location = R './' + fnombre
# Read data in Excel file
# 0 = first tab
DF = pd.read_excel (location, 0)
# mark the data from which text The
df[' file ' = Fnombre
# takes the ' file ' column as the index return
df.set_index ([' File '])
Create a dataframe for each file, and put all the dataframe in a list.
That
Df_list = [DF, DF, DF]
# Create a dataframe list
df_list = [GetFile (fname) for fname in FileNames]
df_list
[ Channel number
file
test1.xlsx 1 255, Channel number
file
TEST2.XLSX 1 255, Channel number
File
test3.xlsx 1 255]
# Merge all the dataframe in the list into a
BIG_DF = Pd.concat (df_list)
BIG_DF
|
Channel |
| Number
File |
|
|
Test1.xlsx |
1 |
255 |
Test2.xlsx |
1 |
255 |
Test3.xlsx |
1 |
255 |
Big_df.dtypes
Channel Int64
number Int64
dtype:object
# Draw a picture
big_df[' Channel '].plot.bar ();