Home  >  Article  >  Backend Development  >  How to Read CSV Files with Semi-Colons as Separators in Pandas?

How to Read CSV Files with Semi-Colons as Separators in Pandas?

Susan Sarandon
Susan SarandonOriginal
2024-10-31 22:00:29350browse

How to Read CSV Files with Semi-Colons as Separators in Pandas?

Separating Data with Semi-Colons in Pandas

When reading a .csv file with pandas, the default separator for columns is a comma (,). However, if your file uses a different separator, such as a semi-colon (;), you'll need to specify this in the parameters when using the read_csv function.

Consider the following .csv file format:

a1;b1;c1;d1;e1;...
a2;b2;c2;d2;e2;...   
.....

To read this file with pandas and split the values into columns based on the semi-colon separator, use the following code:

<code class="python">import pandas as pd

# Specify the file path
csv_path = "C:...."

# Set the separator character to ';'
data = pd.read_csv(csv_path, sep=';')

# Print the resulting DataFrame to check if the values are split into columns
print(data)</code>

By passing sep=';', you're explicitly telling pandas to use the semi-colon as the delimiter. This way, the data will be parsed correctly and organized into individual columns.

The above is the detailed content of How to Read CSV Files with Semi-Colons as Separators in Pandas?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn