229

I'm trying to get a tsv file loaded into a pandas DataFrame.

This is what I'm trying and the error I'm getting:

>>> df1 = DataFrame(csv.reader(open('c:/~/trainSetRel3.txt'), delimiter='\t'))

Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    df1 = DataFrame(csv.reader(open('c:/~/trainSetRel3.txt'), delimiter='\t'))
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 318, in __init__
    raise PandasError('DataFrame constructor not properly called!')
PandasError: DataFrame constructor not properly called!
starball
  • 20,030
  • 7
  • 43
  • 238
screechOwl
  • 27,310
  • 61
  • 158
  • 267

9 Answers9

269

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).

Rick
  • 43,029
  • 15
  • 76
  • 119
huon
  • 94,605
  • 21
  • 231
  • 225
  • 5
    I had some issues with this method - it was very slow and failed indexing at the end. Instead, i used read_table(), which worked much faster and without the extra param. – Yuri Astrakhan Aug 15 '14 at 09:56
  • I get empty 'columns' and the data are a bunch of mess, can this read tab-separated .txt with the header as first line, I guess not. – imrek Aug 31 '15 at 04:59
  • 26
    Note that as of 17.0 `from_csv` is discouraged: use `pd.read_csv` instead! – rafaelvalle Dec 03 '16 at 00:10
  • 2
    I had to use the following: DataFrame.read_csv('filepath.tsv', sep=' ', header=0) – Archie Jan 20 '17 at 09:30
  • 3
    This is a bad answer; you can read TSV natively with `pd.read_csv/read_table`, you just need to set `delim_whitespace=True` or `sep` – smci Apr 29 '18 at 08:31
  • 1
    Downvoted because from_csv is deprecated now and you are stil top answer in google – azerty Nov 13 '18 at 19:51
  • 3
    @rafaelvalle added deprecated notice – Arayan Singh Feb 15 '19 at 21:53
  • FWIW: Here's a warning message that comes up when using from_csv in jupyter notebook (MacPorts version) /opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ipykernel_launcher.py:3: FutureWarning: from_csv is deprecated. Please use read_csv(...) instead. Note that some of the default arguments are different, so please refer to the documentation for from_csv when changing your function calls This is separate from the ipykernel package so we can avoid doing imports until – ATutorMe Jul 29 '19 at 02:38
  • Thanks for sharing this, it works perfectly with `DataFrame.read_csv('c:/~/trainSetRel3.txt', sep='\t')` – Salomon Kabongo Nov 21 '19 at 21:21
  • Also if your first column is an index, `index_col=0` is pretty important too – jimh Jun 07 '21 at 10:31
111

As of 17.0 from_csv is discouraged.

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

Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120
68

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

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Wes McKinney
  • 101,437
  • 32
  • 142
  • 108
25

Try this

df = pd.read_csv("rating-data.tsv",sep='\t')
df.head()

enter image description here

You actually need to fix the sep parameter.

Mohsin Ashraf
  • 972
  • 12
  • 18
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

Antonio Correia
  • 1,093
  • 1
  • 15
  • 22
3
data = pd.read_csv('your_dataset.tsv', delimiter = '\t', quoting = 3)

You can use a delimiter to separate data, quoting = 3 helps to clear quotes in datasst

Đ.J vicky
  • 61
  • 4
2
df = pd.read_csv('filename.csv', sep='\t', header=0)

You can load the tsv file directly into pandas data frame by specifying delimitor and header.

Stefan Ollinger
  • 1,577
  • 9
  • 16
Kofi
  • 1,224
  • 1
  • 10
  • 21
1

use this

import pandas as pd
df = pd.read_fwf('xxxx.tsv')
Emeka Boris Ama
  • 429
  • 4
  • 5
0

Try this:

import pandas as pd
DataFrame = pd.read_csv("dataset.tsv", sep="\t")
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40