Convert CSV to pandas DataFrame
In this pandas tutorial, we will discuss how to convert csv to pandas DataFrame.
Introduction
CSV stands for comma separated value.
DataFrame in pandas is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We will convert CSV to pandas dataframe by using read_csv() method.So we will consider the below csv file for all the examples.
We observed that this file has 4 rows and 4 columns, column names - [college_id,college_name,college_address,Total Staff] and row names are - ['one','two','three','four'].
Example 1: Convert csv to pandas dataframe with no parameters.
In this example, we will convert csv to dataframe without any parameters.
Syntax:
pandas.read_csv("csv_file.csv")
where, csv_file.csv is the file name of csv.
import pandas as pd
#create dataframe from the csv file
data= pd.read_csv("mycsv.csv")
#display the dataframe
print(data)
Output:
college_id college_name college_address Total Staff
0 c-001 vignan university guntur 1200
1 c-021 vvit guntur 3422
2 c-002 RVR - JC guntur 5644
3 c-004 Andhra University guntur 670
Example 2: Convert csv to pandas dataframe by setting custom index.
In this example, we will convert csv to dataframe by setting the custon index(from cvs file).Sp this column will act as index for thr dataframe. The parameter that is used to set the custom index is 'index_col'.
Syntax:
pandas.read_csv("csv_file.csv",index_col=column_name)
where, csv_file.csv is the file name of csv and column_name is the column label in csv which will be the index in pandas dataframe.
Here we will set the college_id column as custom index.
import pandas as pd
#create dataframe from the csv file
data= pd.read_csv("mycsv.csv",index_col='college_id')
#display the dataframe
print(data)
Output:
college_name college_address Total Staff
college_id
c-001 vignan university guntur 1200
c-021 vvit guntur 3422
c-002 RVR - JC guntur 5644
c-004 Andhra University guntur 670
Example 3: Convert csv to pandas dataframe by setting column labels.
In this example, we will convert csv to dataframe by setting the column labels using names parameter through list.
Syntax:
pandas.read_csv("csv_file.csv",names=[column_names])
where, csv_file.csv is the file name of csv and set column labels as - ['col1','col2','col3','col4']
import pandas as pd
#create dataframe from the csv file
data= pd.read_csv("mycsv.csv",names=['col1','col2','col3','col4'])
#display the dataframe
print(data)
Output:
col1 col2 col3 col4
0 college_id college_name college_address Total Staff
1 c-001 vignan university guntur 1200
2 c-021 vvit guntur 3422
3 c-002 RVR - JC guntur 5644
4 c-004 Andhra University guntur 670
Example 4: Convert csv to pandas dataframe by setting top rows.
In this example, we will convert csv to dataframe by setting only top n rows through nrows paramater. It will take an integer value and display rows from the top.
Syntax:
pandas.read_csv("csv_file.csv",nrows=n)
where, csv_file.csv is the file name of csv and display the top 3 rows.
import pandas as pd
#create dataframe from the csv file
data= pd.read_csv("mycsv.csv",nrows=3)
#display the dataframe
print(data)
Output:
college_id college_name college_address Total Staff
0 c-001 vignan university guntur 1200
1 c-021 vvit guntur 3422
2 c-002 RVR - JC guntur 5644
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 14,2024