Different ways to import csv file in Pandas


We can use import different data files in pandas like csv, excel, JSON, SQL etc. In pandas library, we have different ways to import the csv files into our python working environment.

CSV is abbreviated as Comma Separated Values. This is the file format most widely used in the Data Science. This stores the data in a tabular format where the column holds the data fields and rows holds the data. Each row in the csv file is separated by a comma or by a delimiter character which can be customized by the user. We have to use the pandas library to work with the csv files in data science.

Using read_csv() function

We can create the dataframe from the data of a csv file. In pandas library, we have a function named read_csv() to read the csv file data. The following is the syntax for creating the dataframe from the csv file.

pandas.read_csv(csv_file)

Where,

  • pandas is the name of the library.

  • read_csv is the function.

  • csv_file is the input csv file.

Example

Here in this example we will create the pandas dataframe from a csv file data by using the read_csv() function. The following is the code for reference.

import pandas as pd
data=pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")
print(data.head(10))

Output

   PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked
0            1         0       3  ...   7.2500   NaN         S
1            2         1       1  ...  71.2833   C85         C
2            3         1       3  ...   7.9250   NaN         S
3            4         1       1  ...  53.1000  C123         S
4            5         0       3  ...   8.0500   NaN         S
5            6         0       3  ...   8.4583   NaN         Q
6            7         0       1  ...  51.8625   E46         S
7            8         0       3  ...  21.0750   NaN         S
8            9         1       3  ...  11.1333   NaN         S
9           10         1       2  ...  30.0708   NaN         C

[10 rows x 12 columns]

Using pandas.read_table() function

When we want to read the data of csv files and other type of files in a general manner by using the read_table() function of the pandas library. The following is the syntax for using the read_table() function.

pandas.read_table(csv_file)

Example

If we want to access the csv file data then we can pass the csv file as the input argument to the read_table() function of the pandas library. The following is the code.

import pandas as pd
data=pd.read_table("https://raw.githubusercontent.com/Opensourcefordatascience/Data-sets/master/blood_pressure.csv",sep = ',')
print(data.head(20)) 

Output

    patient   sex agegrp  bp_before  bp_after
0         1  Male  30-45        143       153
1         2  Male  30-45        163       170
2         3  Male  30-45        153       168
3         4  Male  30-45        153       142
4         5  Male  30-45        146       141
5         6  Male  30-45        150       147
6         7  Male  30-45        148       133
7         8  Male  30-45        153       141
8         9  Male  30-45        153       131
9        10  Male  30-45        158       125
10       11  Male  30-45        149       164
11       12  Male  30-45        173       159
12       13  Male  30-45        165       135
13       14  Male  30-45        145       159
14       15  Male  30-45        143       153
15       16  Male  30-45        152       126
16       17  Male  30-45        141       162
17       18  Male  30-45        176       134
18       19  Male  30-45        143       136
19       20  Male  30-45        162       150

Using pandas.DataFrame.from_csv() function

This function DataFrame.from_csv() is similar to the read_csv() function. The following is the syntax for using the DataFrame.from_csv() function.

pandas.DataFrame.from_csv(csv_file)

This function is available in previous versions of python but not in present versions.

Updated on: 20-Oct-2023

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements