Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn CSV Files | Reading Files in Pandas
Pandas First Steps

Swipe to show menu

book
CSV Files

Since pandas is the go-to library for data analysis and manipulation, one of its key features is its ability to read and write various file types, including CSV files.

A CSV (Comma-Separated Values) file is a plain text file used to store tabular data, where each row represents a record, and columns are separated by commas.

A CSV file can contain the following data:

  • Numbers: integer or decimal values (e.g., 42, 3.14);

  • Text: strings or categorical data (e.g., John, Active);

  • Dates/Times: timestamps (e.g., 2023-12-30);

  • Booleans: logical values (True, False).

Each row must have the same number of columns, and the first row often contains column headers.

Functions like read_csv() and to_csv() come in handy for dealing with CSV data.

The basic syntax of read_csv() and key parameters are as follows:

python
  • filepath_or_buffer: path to the CSV file (string or URL);

  • sep: delimiter (default is a comma ,);

  • header: row number to use as the column headers (default is the first row);

  • names: List of column names to use;

  • usecols: olumns to read (subset of columns).

12345
# Loading the CSV into a `DataFrame` import pandas as pd salary_data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a43d24b6-df61-4e11-9c90-5b36552b3437/Salary+Dataset.csv') print(salary_data)
copy

Note

Make sure that the dataset link is wrapped in quotation marks.

The basic syntax of to_csv() and key parameters are as follows:

python
  • path_or_buf: file path or object where the CSV should be written;

  • sep: delimiter for separating values (default is a comma ,);

  • columns: subset of columns to write (default is all columns);

  • header: whether to include column names as the header (default is True);

  • index: whether to write row indices to the file (default is True).

1234567
import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) countries.to_csv('countries.csv') print('Done')
copy
Task

Swipe to start coding

You are given a URL to a CSV file stored as a string in the file_url variable.

  • Read the CSV file from the given URL into a DataFrame named wine_data.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
CSV Files

Since pandas is the go-to library for data analysis and manipulation, one of its key features is its ability to read and write various file types, including CSV files.

A CSV (Comma-Separated Values) file is a plain text file used to store tabular data, where each row represents a record, and columns are separated by commas.

A CSV file can contain the following data:

  • Numbers: integer or decimal values (e.g., 42, 3.14);

  • Text: strings or categorical data (e.g., John, Active);

  • Dates/Times: timestamps (e.g., 2023-12-30);

  • Booleans: logical values (True, False).

Each row must have the same number of columns, and the first row often contains column headers.

Functions like read_csv() and to_csv() come in handy for dealing with CSV data.

The basic syntax of read_csv() and key parameters are as follows:

python
  • filepath_or_buffer: path to the CSV file (string or URL);

  • sep: delimiter (default is a comma ,);

  • header: row number to use as the column headers (default is the first row);

  • names: List of column names to use;

  • usecols: olumns to read (subset of columns).

12345
# Loading the CSV into a `DataFrame` import pandas as pd salary_data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a43d24b6-df61-4e11-9c90-5b36552b3437/Salary+Dataset.csv') print(salary_data)
copy

Note

Make sure that the dataset link is wrapped in quotation marks.

The basic syntax of to_csv() and key parameters are as follows:

python
  • path_or_buf: file path or object where the CSV should be written;

  • sep: delimiter for separating values (default is a comma ,);

  • columns: subset of columns to write (default is all columns);

  • header: whether to include column names as the header (default is True);

  • index: whether to write row indices to the file (default is True).

1234567
import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) countries.to_csv('countries.csv') print('Done')
copy
Task

Swipe to start coding

You are given a URL to a CSV file stored as a string in the file_url variable.

  • Read the CSV file from the given URL into a DataFrame named wine_data.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt