Home > Article > Backend Development > 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!