Pandas Library: Importing Excel CSV file

Using Python Pandas Library to Import CSV file

  1. Import the Pandas Library into your project – here I rename it to “pd”
  2. Use Pandas to Read the CSV file
  3. Print the top 10 rows including the column headers.
>import pandas as pd
>variablename_df = pd.read_csv("filename.csv")

Import Pandas:

The code above starts out by importing the pandas library using the “import pandas” script. I rename the pandas library to pd using the “as pd” code. You can use the “as” word to change the name you use throughout the rest of your program. Most of the time you will use this to shorten the name of the library. Instead of typing pandas every time you need it, you can now just type “pd” in its place.

Importing the .CSV file:

Next think of a relevant variable name to store our csv file into the program. We will use this file name frequently in the program so you want to make it very specific as you may need it many times. You will also use similar names as you breakdown the information so using a broad title works well here.
The “df” attached to the “variablename” is not necessary but it helps you reference it and also let’s developers who come behind you know that this is a DataFrame (2D array). Again you don’t have to use the “df” name at the end of the variable but it is common coding practice to do so. next we use the “=” to let the interpreter know what will go into that variable we just created. In this case we will be passing the Excel file into it using the “pd.read_csv()” function. The “pd” calls the pandas library and the “read_csv()” portion is a function of the pandas library. In the “( )” you will call the file name or location on you computer. Sometimes you will put the file in the same folder as the .py file you are working on -> “excel.csv”. If you have the file on a server or somewhere else then you will need to build a path to the file example: (../excel.csv)

Printing the Results:

The last part is just printing the results. Here we can check to see if the program will actually pull the file so we can see what data we have in the prompt. Here we call the first 10 rows of the document by using the “.head()” function, which by default pulls the first 5 rows. Here I used 10 so I could see the first 10 rows.

Leave a Reply

Your email address will not be published. Required fields are marked *