Issue
I'm new to python and pandas. 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!
Solution
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
).
Answered By - huon Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.