The Python Oracle

How to load a tsv file into a Pandas DataFrame?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Unforgiving Himalayas Looping

--

Chapters
00:00 Question
00:33 Accepted answer (Score 254)
01:13 Answer 2 (Score 111)
01:35 Answer 3 (Score 67)
01:50 Answer 4 (Score 25)
02:05 Thank you

--

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

Accepted answer links:
[.read_csv]: https://pandas.pydata.org/pandas-docs/st...
[the ]: http://pandas.pydata.org/pandas-docs/dev...

Answer 2 links:
[from_csv]: http://pandas.pydata.org/pandas-docs/ver...

Answer 3 links:
[pandas.read_table(filepath)]: https://pandas.pydata.org/docs/reference...

Answer 4 links:
[image]: https://i.stack.imgur.com/EfaO5.png

--

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