How to Import CSV Data in Python

with No Comments

To Import CSV data into your Python project, you should have your working directory set correctly first. If you have not done that, click here to read how to set your working folder in python.

So before I start, i know where is my working folder and where is my CSV file.

how to import csv - set folder

 

Do this, and I know I should place my CSV file at C:\Users\Public\Machine Learning

 

Pandas is an excellent library for data handling. If your python do not have pandas installed, do this in your terminal

 

Python Library that Import CSV

pip install pandas

 

Back to your editor or console, we are going to import panadas

 

import pandas as pd

 

Now we can use call the functions from pandas using “pd”

Importing CSV Data to Python

dataset = pd.read_csv(“Data.csv”)

 

This will set declare “dataset” as a variable in your project that equals to your data in CSV file

What we have just did is that we called pandas (pd) function name “read_csv”.

“Data.csv” is my csv file. Change it to your CSV file name, and this should work for you.

Visualize the CSV data you have imported

If you are using Anaconda Spyder, you should see your variables in the “variable explorer” section.

Double click the dataset you have just declared, and you should visualize your data.

 

how to import csv - dataset

 

Other Sections on Data Handling in Python

1.) How to Import Libraries 

2.) How to Know and Change the Working Directory 

3.) How to Import CSV Data using Pandas

4.) How to Set Dependent Variables and Independent Variables using iloc

5.) How to Handle Missing data with Imputer

6.) How to Set Categorical Data (Dummy Variable) using LabelEncoder and OneHotEncoder

7.) How to Split Data into Training Set and Testing Set

8.) How to Apply Feature Scaling

Leave a Reply