Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a file with a semi colon separator in pandas

Tags:

python

pandas

csv

I a importing a .csv file in python with pandas.

Here is the file format from the .csv :

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

here is how get it :

from pandas import * csv_path = "C:...." data = read_csv(csv_path) 

Now when I print the file I get that :

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

And so on... So I need help to read the file and split the values in columns, with the semi color character ;.

like image 255
Jean Avatar asked Jul 07 '14 08:07

Jean


People also ask

How do you use the separator in pandas?

pandas. read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infer', names=None, index_col=None, ....) 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.

What is delimiter panda?

Pandas provide a method to split string around a passed separator/delimiter. After that, the string can be stored as a list in a series or it can also be used to create multiple column data frames from a single separated string.

Which argument do you specify with read_csv () to specify a separator character?

The argument delim_whitespace controls whether or not whitespace (e.g. ' ' or ' ' ) will be used as separator. See pandas. read_csv for details.

Do pandas files need to be comma separated?

Pandas - Space, tab and custom data separators Data files need not always be comma separated. Space, tabs, semi-colons or other custom separators may be needed. Consider storing addresses where commas may be used within the data, which makes it impossible to use it as data separator.

How to read a text file with pandas?

We will read the text file with pandas using the read_csv () function. Along with the text file, we also pass separator as a single space (‘ ’) for the space character because, for text files, the space character will separate each field.

How to read a CSV file with a field separator in Excel?

For Excel to be able to read a CSV file with a field separator used in a given CSV file, you can specify the separator directly in that file. For this, open your file in any text editor, say Notepad, and type the below string before any other data:

Can I use commas as data separators in read_CSV?

Consider storing addresses where commas may be used within the data, which makes it impossible to use it as data separator. Let us examine the default behavior of read_csv (), and make changes to accommodate custom separators. The default separator for read_csv () is comma.


1 Answers

read_csv takes a sep param, in your case just pass sep=';' like so:

data = read_csv(csv_path, sep=';') 

The reason it failed in your case is that the default value is ',' so it scrunched up all the columns as a single column entry.

like image 114
EdChum Avatar answered Oct 04 '22 14:10

EdChum