In this article we will discuss how to read a CSV file with different type of delimiters to a Dataframe.

Python’s Pandas library provides a function to load a csv file to a Dataframe i.e.

It reads the content of a csv file at given path, then loads the content to a Dataframe and returns that. It uses comma (,) as default delimiter or separator while parsing a file. But we can also specify our custom separator or a regular expression to be used as custom separator.

To use pandas.read_csv() import pandas module i.e.

Using read_csv() with custom delimiter

Suppose we have a file ‘users.csv‘ in which columns are separated by string ‘__’ like this.
Contents of file users.csv are as follows,

Now to load this kind of file to a dataframe object using pandas.read_csv() we have to pass the sep & engine arguments to pandas.read_csv() i.e.

Output:

Here, sep argument will be used as separator or delimiter. If sep argument is not specified then default engine for parsing ( C Engine) will be used which uses ‘,’ as delimiter. So, while specifying the custom sep argument we must specify the engine argument as ‘python’, otherwise we will get warning like this,

You can avoid this warning by specifying engine=’python’.

Using read_csv() with white space or tab as delimiter

As we have seen in above example, that we can pass custom delimiters. Now suppose we have a file in which columns are separated by either white space or tab i.e.
Contents of file users_4.csv are,

Now, to load this kind of file to dataframe with pandas.read_csv() pass ‘\s+’ as separator. Here \s+ means any one or more white space character.

Contents of the dataframe returned are,

Using read_csv() with regular expression for delimiters

Suppose we have a file where multiple char delimiters are used instead of a single one. Like,

Contents of file users_5.csv are,

Now, to load this kind of file to dataframe with read_csv() pass a regular expression i.e. ‘[:,|_]’ in sep argument. This regular expression means use any of these characters ( , : | ) asa delimiter or separator i.e.

Output:

Complete example is as follows:

Output:

 

 

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.