Learning Pandas (11)

Source: Internet
Author: User

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
Number
Channel
0 1 255
# 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
Number
Channel
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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.