Importing Dataset in Pandas
Start your free 7-days trial now!
To import and read a CSV file as a DataFrame, use Pandas read_csv(~)
method.
read_csv(~)
is a misnomer. Despite having csv
in its name, the method can be used to import datasets that use delimiters (separators) other than comma.
Datasets with Column Labels and Indices
Suppose we wanted to import the following file called my_file.csv
:
a,b,cA,1,2,3B,4,5,6
Note the following:
the column labels are
a
,b
andc
.the row labels are
A
andB
.
To read this file as a DataFrame:
Note that this code assumes the file to be located in the same directory.
Datasets with Only Values
Suppose our CSV dataset only consisted of values:
1,2,34,5,6
To import this file, we need to add the header=None
option:
As we can see, Pandas uses the default integer indices for its column and row labels.
Datasets with Custom Delimiters
The read_csv()
method can also be used to import datasets that use delimiters other than comma. We can specify the delimiter by supplying the delimiters
parameter.
As an example, suppose our dataset uses a single blank space as the delimiter:
a b cA 1 2 3B 4 5 6
To read this file: