Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas read csv with extra commas in column

Tags:

I'm reading a basic csv file where the columns are separated by commas with these column names:

userid, username, body

However, the body column is a string which may contain commas. Obviously this causes a problem and pandas throws out an error:

CParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 8

Is there a way to tell pandas to ignore commas in a specific column or a way to go around this problem?

like image 785
David Avatar asked Sep 23 '15 15:09

David


People also ask

How do I handle extra commas in a CSV file?

You need to specify text qualifiers. Generally a double quote (") is used as text qualifiers. All the text is always put inside it and all the commas inside a text qualifier is ignored. This is a standard method for all CSV, languages and all platforms for properly handling the text.

How do you handle commas in data in a CSV file in Python?

A CSV file is a simple text file where each line contains a list of values (or fields) delimited by commas. Although the term "Comma" appears in the format name itself, but you will encounter CSV files where data is delimited using tab ( \t ) or pipe ( | ) or any other character that can be used as a delimiter.

How read comma separated CSV file in pandas?

The pandas DataFrame class supports serializing and de-serializing of CSV in an extenstive way through the read_csv() method. The read_csv() method of pandas DataFrame class reads a CSV file and loads each record as a row in the DataFrame.

What does parse_dates in pandas do?

If True and parse_dates is enabled, pandas will attempt to infer the format of the datetime strings in the columns, and if it can be inferred, switch to a faster method of parsing them. In some cases this can increase the parsing speed by 5-10x.


1 Answers

Imagine we're reading your dataframe called comma.csv:

userid, username, body 01, n1, 'string1, string2' 

One thing you can do is to specify the delimiter of the strings in the column with:

df = pd.read_csv('comma.csv', quotechar="'") 

In this case strings delimited by ' are considered as total, no matter commas inside them.

like image 128
Fabio Lamanna Avatar answered Oct 18 '22 04:10

Fabio Lamanna