Notice: Constant automattic\jetpack\extensions\social_previews\FEATURE_NAME already defined in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/jetpack/extensions/blocks/social-previews/social-previews.php on line 14
How To Create Dataframe From CSV File In Python - Dggul AI Tutorial

How To Create Dataframe From CSV File In Python


Notice: Undefined index: limited_lang in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/code-syntax-highlighter/inc/src/rendrer.php on line 297

Welcome you all in how to create dataframe from csv file in python Tutorial. In this tutorial, we will learn to create pandas dataframe from CSV file in python.

In previous tutorial we have learned to create dataframe from list. If you want to revise, you can check it once again.

How To Create Pandas Dataframe From List In Python

A Comma Separated Values (CSV) file is a plain text file that contains a list of data. These files are often used for exchanging data between different applications.

In data science we have to deal with massive amount of data. Sometime these data are stored in CSV files and we have to analyze these csv files. Pandas is a very popular python library which is used in data science.

Pandas DataFrame is nothing but an in-memory representation of excel like data. In this tutorial, we will learn different ways to create pandas dataframe from CSV file in python. So let’s get started.

How To Create Dataframe From CSV File In Python

We can create pandas dataframe from CSV files in multiple ways. In short, pandas dataframe from csv files can be created using read_csv(), read_table() functions and using csv module. So let’s see each of them one by one to read csv file in python pandas.

Here, I am using water quality dataset for example. You can download this dataset from here.

I am using Jupyter Notebook for coding. If you don’t know how to work on Jupyter notebook then I have written an in-depth tutorial on Jupyter notebook. You can learn it from here.

Using CSV Module

We can use csv module to create dataframe from csv files. To do so, csv.reader() method is used to read the csv file then from this csv file we can create dataframe. So let’s understand it practically with an example.

import pandas as pd

#import csv module
import csv

#open csv 
with open('water_potability.csv') as csv_file:

     #call reader() method to read csv file
    csv_reader = csv.reader(csv_file)

     #creating dataframe
    df = pd.DataFrame([csv_reader])

for index, row in df.iterrows():
    print(row)

What We Did ?

  • First of all we have imported pandas module then imported csv module.
  • Then opened the csv file as a csv file. My csv file is stored in the same directory so I don’t have to specify the path but if you have stored your csv file in another location then you must have to specify the proper path.
  • Then we have called reader() method of csv module which return a reader object which will iterate over lines in the given csv file.
  • Now from this csv file we have created dataframe.
  • And at the last we have printed the dataframe.

Output :

how to create dataframe from csv file in python

Using read_csv() method

We can also create pandas dataframe from csv file using read_csv() method. Pandas provide read_csv() method to read csv file and create dataframe.

Creating dataframe using read_csv() method is very simple, you just have to import pandas library and call read_csv() method to create dataframe. 

import pandas as pd 

# creating a data frame 
df = pd.read_csv("water_potability.csv") 

#Display dataframe
print(df)

Output

how to create dataframe from csv file in python

If we want to display only first 10 dataset of dataframe then we have to call head() method. head() method is used to get the first n rows. Let’s see the below code.

import pandas as pd
  
# creating a data frame
df = pd.read_csv("water_potability.csv")

# printing first 10 rows of dataframe
print(df.head(10))

Output

how to create dataframe from csv file in python

 

Using read_table() Method

Another simple way of creating dataframe from csv file is read_table() method. read_table() method read general delimited file into DataFrame.

import pandas as pd
  
# creating a data frame
df = pd.read_table("water_potability.csv")

print(df)

What We Did ?

  • First of all imported pandas.
  • Then called read_table() method to create dataframe. We have passed csv file as argument.
  • And at last printed the dataframe.

Output

how to create dataframe from csv file in python

And now to display first 16 rows of the dataframe, we have called head() method and passed the no of rows which we want to display. 

import pandas as pd
  
# creating a data frame
df = pd.read_table("water_potability.csv")

# Printing first 16 rows of the dataframe
print(df.head(16))

Output

So guys it is all about how to create dataframe from csv file in python tutorial. I hope you have understand well to create dataframe from csv files. If you are getting any error or problem then ask me in comment section. In our upcoming tutorials we will learn to create pandas dataframe from dictionary and many more till then stay tuned with Dggul AI Tutorial.

Leave a Comment