Read CSV into a DataFrame

Summary: in this tutorial, you’ll learn how to create a DataFrame directly from CSV files.

CSV stands for comma-delimited values and is the most common format for storing data. You can use most any other type of delimiter-based file, for example, TSV – tab-separated values.

We can even create DataFrame with simple TXT files, if the data inside is separated with a delimiter.

Read CSV

For reading CSV, we use Pandas read_csv method.

import pandas as pd dframe = pd.read_csv("example.csv") dframe
Code language: Python (python)

Output :


Read TSV

TSV is just another variation of CSV, specifically it use a tab as the delimiter instead of a comma.

In order to read TSV into a DataFrame, we use the same read_csv method, but pass an additional delimiter parameter.

import pandas as pd dframe = pd.read_csv("example.tsv", delimiter="\t") dframe
Code language: Python (python)

In this example, \t is the Python way of writing a tab. The output is still the same.

Leave a Comment