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
Output :
name | continent | area | population | |
---|---|---|---|---|
Afghanistan | Asia | 652.23 | 25.500.100 | |
1 | Albania | Europe | 28.748 | 2.831.741 |
2 | Algeria | Africa | 2.381.741 | 37.100.000 |
3 | Andorra | Europe | 468 | 78.115 |
4 | Angola | Africa | 1.246.700 | 20.609.294 |
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
In this example, \t
is the Python way of writing a tab. The output is still the same.
