Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - pandas.DataFrame.from_csv vs pandas.read_csv

Tags:

python

pandas

csv

What's the difference between:

pandas.DataFrame.from_csv, doc link: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_csv.html

and

pandas.read_csv, doc link: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html

like image 341
user3659451 Avatar asked Oct 21 '14 20:10

user3659451


People also ask

What is the difference between read_csv and to_csv?

It is preferable to use the more powerful pandas. read_csv() for most general purposes, but from_csv makes for an easy roundtrip to and from a file (the exact counterpart of to_csv), especially with a DataFrame of time series data.

What is the difference between read_table and read_csv in pandas?

The difference between read_csv() and read_table() is almost nothing. In fact, the same function is called by the source: read_csv() delimiter is a comma character. read_table() is a delimiter of tab \t .

Is read_csv a Dataframe?

read_csv is used to load a CSV file as a pandas dataframe. In this article, you will learn the different features of the read_csv function of pandas apart from loading the CSV file and the parameters which can be customized to get better output from the read_csv function.


1 Answers

There is no real difference (both are based on the same underlying function), but as noted in the comments, they have some different default values (index_col is 0 or None, parse_dates is True or False for read_csv and DataFrame.from_csv respectively) and read_csv supports more arguments (in from_csv they are just not passed through).

Apart from that, it is recommended to use pd.read_csv.
DataFrame.from_csv exists merely for historical reasons and to keep backwards compatibility (plans are to deprecate it, see here), but all new features are only added to read_csv (as you can see in the much longer list of keyword arguments). Actually, this should be made more clear in the docs.

like image 93
joris Avatar answered Sep 29 '22 13:09

joris