Python csv module vs pandas.read_csv and Python xlrd vs pandas.read_excel

Viewed 3571

In python, we can use either the csv module or the pandas.read_csv function to handle csv files. For Excel files we can use the xlrd module or the pandas.read_excel function.

I use pandas a lot and I feel that the read_csv and read_excel functions come in handy for me. Can anyone explain me what are the pros and cons of each of these methods?

1 Answers

Important note: xlrd No longer supports .xlsx files!

This change happened in version 2.0.0

However regarding CSV files I think it would be interesting to do a speed comparison between pandas and csv.

My experience is that the pandas module reads CSV files more strictly. If a column is all numbers and one row is empty, pandas produces a NaN value unless you use df.fillna('', inplace=True) or something similar. This can be annoying if you have mixed data types or if you expect None or blank values in a column. The csv library seems to handle this a little nicer.

When I used csv on python2, utf-8 was a nightmare, but I assume that's better now with python3.

Related