Home > Backend Development > Python Tutorial > 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
Release: 2024-10-31 22:00:29
Original
466 people have browsed it

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;...   
.....
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template