Welcome to TechNote

Importing Data

Importing CSV Files

import pandas as pd
pd.read_csv('fileName.csv')

The pd stands for the Pandas library which we have just imported. The use of the variable pd is arbitrary, however we will follow the convention of using pd.

Importing a CSV File Saved in a Different Folder Location

import pandas as pd
pd.read_csv(r'/Users/UserName/fileName.csv')

The r stands for raw and it causes backslashes to be interpreted as regular text rather than as special characters. It is only necessary when we use backslashes, however we included this in our example just to avoid confusion.

Specify a Header Row

import pandas as pd
pd.read_csv('fileName.csv', header=1)

In our example dataset, the first row is already our header row, so it’s not necessary to indicate the parameter and argument header = 0. If the first row of our dataset appeared on the third row, we can use header = 2. I have encountered this in most reports generated using some reporting software like Business Objects or Excel data downloaded from Bloomberg.

Importing Excel Files

import pandas as pd
pd.read_excel('fileName.xlsxv)

Importing an Excel File Saved in a Different Folder Location

import pandas as pd
pd.read_excel(r'/Users/UserName/fileName.xlsx')

Using a Variable File Path

import os
filePath = r'/Users/UserName/fileName.csv'
fileName = 'fileName.csv'
directory = os.path.join(filePath,fileName)

Adding Folders to a Variable File Path

import os
filePath = r'/Users/UserName/fileName.csv'
folderName = 'myFolder'
fileName = 'fileName.csv'
directory = os.path.join(filePath,folderName,fileName)