241

I'm trying to get a TAB-delimited (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!
2
  • 17
    For those coming to this answer in 2017+, use read_csv('path_to_file', sep='\t'). See this answer below Commented Nov 6, 2017 at 16:49
  • read_csv defaults to comma as the separator, so read_table is more convenient for TSV. Commented Oct 1, 2023 at 21:40

9 Answers 9

295

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

Sign up to request clarification or add additional context in comments.

10 Comments

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.
Note that as of 17.0 from_csv is discouraged: use pd.read_csv instead!
I had to use the following: DataFrame.read_csv('filepath.tsv', sep=' ', header=0)
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
@rafaelvalle added deprecated notice
|
112

As of 17.0 from_csv is discouraged.

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

2 Comments

Note: read_table is deprecated since version 0.24.0. Use pandas.read_csv() instead.
Apparently read_table was later un-deprecated in 0.25.0.
72

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

1 Comment

read_table doesn't require any parameters. Perfectly working.
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.

Comments

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

Comments

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

Comments

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.

Comments

1

use this

import pandas as pd
df = pd.read_fwf('xxxx.tsv')

1 Comment

Why this instead of read_csv with sep='\t'?
0

Try this:

import pandas as pd
DataFrame = pd.read_csv("dataset.tsv", sep="\t")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.