The Python Oracle

How to load a tsv file into a Pandas DataFrame?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Island

--

Chapters
00:00 How To Load A Tsv File Into A Pandas Dataframe?
00:27 Accepted Answer Score 288
00:57 Answer 2 Score 70
01:08 Answer 3 Score 112
01:25 Answer 4 Score 9
01:37 Thank you

--

Full question
https://stackoverflow.com/questions/9652...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #csv

#avk47



ACCEPTED ANSWER

Score 288


The .read_csv function does what you want:

pd.read_csv('c:/~/trainSetRel3.txt', sep='\t')

If you have a header, you can pass header=0.

pd.read_csv('c:/~/trainSetRel3.txt', sep='\t', header=0)

Note: Prior 17.0, pd.DataFrame.from_csv was used (it is now deprecated and the .from_csv documentation link redirects to the page for pd.read_csv).




ANSWER 2

Score 112


As of 17.0 from_csv is discouraged.

Use pd.read_csv(fpath, sep='\t') or pd.read_table(fpath).




ANSWER 3

Score 70


Use pandas.read_table(filepath). The default separator is tab.




ANSWER 4

Score 9


open file, save as .csv and then apply

df = pd.read_csv('apps.csv', sep='\t')

for any other format also, just change the sep tag