Instagram
youtube
Facebook
Twitter

Loading Data from CSV

Comma Separated Variables (CSV)

  • We know how to create DataFrame but most of the time we will be working with already existing datasets and CSV is one of the most common formats for bid datasets.
  • CSV (comma-separated values) is a text-only spreadsheet format.
  • The first row of CSV contains the heading and other rows contain values, whereas each column heading and each variable is separated by a comma.
    column1,column2,column3
    value1,value2,value3

Loading CSV Data

  • You can load the data of CSV File into a DataFrame in Pandas using:
    pd.read_csv('file.csv')

    Here the read_csv() method is called and 'file.csv' is the name of the CSV file and is passed as an argument.

  • You can also save the data to a CSV file from a DataFrame using:

    df.to_csv('csv-file.csv')

    Here, the to_csv() method is used and 'df' is our DataFrame object and 'csv-file.csv' is the file where we are saving the data of the dataframe.

Example:

Here, we have a small CSV file, which contains a dataset of the top 5 cleanest cities in India and its columns contain City, Population (in Millions), and its Rank.

import pandas as pd

df = pd.read_csv('sample.csv')
print(df)

 Output:

City Population Rank
Indore 32.0 1
Surat 77.0 2
Vijayawada 21.0 3
Navi Mumbai 11.0 4
Pune 7.4 5