Specify Delimiter when Reading pandas DataFrame from CSV File in Python (Example)


In this article you’ll learn how to change the separator when importing a pandas DataFrame from a CSV file in Python.

The tutorial will contain these content blocks:

Here’s how to do it…


Example Data & Software Libraries

First, we need to import the pandas library:

import pandas as pd                              # Import pandas library in Python

As a next step, let’s also create some example data in Python:

data = pd.DataFrame({'x1':range(1, 8),           # Create pandas DataFrame
                     'x2':['x', 'y', 'y', 'z', 'z', 'y', 'z'],
                     'x3':range(18, 11, - 1),
                     'x4':['a', 'b', 'c', 'd', 'e', 'f', 'g']})

We can now write this pandas DataFrame to a CSV file using the to_csv function:

data.to_csv('data.csv',                          # Export pandas DataFrame to CSV
            index = False,
            sep = ';')

If we would now load this CSV file into Python with the default separator specifications of the read_csv function, the output would look as shown below:

data_import_default = pd.read_csv('data.csv')    # Import CSV file
print(data_import_default)                       # Print imported CSV file
#   x1;x2;x3;x4
# 0    1;x;18;a
# 1    2;y;17;b
# 2    3;y;16;c
# 3    4;z;15;d
# 4    5;z;14;e
# 5    6;y;13;f
# 6    7;z;12;g

As you can see, our imported pandas DataFrame is not shown properly. Let’s fix this!


Example: Specify Separator when Importing a pandas DataFrame from a CSV File

This example shows how to set an appropriate delimiter when reading a CSV file as pandas DataFrame to Python.

For this task, we can use the sep argument as shown below. In this specific case, we are using a semicolon instead of a comma as separator:

data_import_sep = pd.read_csv('data.csv',        # Import CSV file
                              sep = ';')
print(data_import_sep)                           # Print imported CSV file


table 1 DataFrame specify delimiter when reading pandas dataframe from csv file python


Table 1 shows the structure of our example data: It contains seven rows and four variables. As you can see, it has been imported properly.


Video, Further Resources & Summary

Some time ago, I have published a video on my YouTube channel, which demonstrates the topics of this article. You can find the video below:


The YouTube video will be added soon.


Furthermore, you may read some of the related Python articles on this homepage:


To summarize: In this tutorial you have learned how to use a different delimiter when importing a pandas DataFrame from a CSV file in Python. Please tell me about it in the comments, in case you have further questions.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.