Blog

This article provides details of various ways to 'Import' different forms of data ( excel , csv , tab delimited , Rdata etc ) into your Python Environment.

Pre-requisite : To use the various functions/methods described below , you need to have the 'pandas' package installed and imported into your Python Environment.

Importing pandas :

                     import pandas as pd

1) Importing a CSV ( Comma Separated Value ) file from your local system- 

                     Import_DF = pd.read_csv("C:\Users\file1.csv")

or if the csv file is available in the working directory , then you can use the following code 
                    Import_DF = pd.read_csv("file1.csv")

Note : 

1.a) By default , read_csv() assumes that your data has header record , in case your data does not contain header data , then use the following code -
                   Import_DF = pd.read_csv("file1.csv" , header = None)

1.b) Changing / Adding the Column Names while importing the data using read_csv()
                   Import_DF = pd.read_csv("file1.csv" , header = None , names =['A' , 'B'])


2) Importing a CSV file from a Website / URL - 

                  Web_data  = pd.read_csv("http://winterolympicsmedals.com/medals.csv")


3) Importing a TXT file  - 

                  Txt_data = pd.read_table("Text_Data.txt")


4) Importing a Tab Delimited / Separated file  - 

                  Tab_Sep_data = pd.read_csv("Tab_Separated_Data.txt", sep =" ")


5) Importing an Excel file  - 

                  Excel_DF = pd.read_excel('Excel_Data.xls',skiprows=1,sheet_name='Concrete')

Explanation of the Parameters used - 
          Skiprows : defines the number of rows in the excel file at the start which has to be skipped.
          sheet_name : mention the name of the sheet in excel which has to be read 


Happy Learning !!
Priyaranjan Mohanty
@AUTHOR : Admin

Tags:Eco, Water, Air, Environment

Comments (0)

    No Comments Found
Leave a Comment