Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to read and write to csv files: pandas functions vs csv library?

Tags:

python

csv

I'm doing some exercises on Udacity.

With the pandas library dealing with csv files seems super intuitive. After importing pandas as pd I can just do:

Read file

pd.read_csv("file_path")

Write file

pd.to_csv("file name")

In comparison if I import the csv library the code seems unintuitive. I have to do this:

Read file

with open("file_path", 'rb') as f:
            reader = csv.reader(f)

Write file

with open("file name", 'wb') as f:
    writer = csv.writer(f)

Why should I choose the csv library if if pandas read/write is so straighforward?

like image 374
megashigger Avatar asked Sep 16 '25 01:09

megashigger


1 Answers

You should use whatever works best for you! Csv is native, so it's pretty easy to move your code to other platforms or machines. That might be an advantage in some circumstances. But if you're comfortable with panda, you should definitely use it!

like image 120
Dr Xorile Avatar answered Sep 17 '25 14:09

Dr Xorile