问题:如何将tsv文件加载到Pandas DataFrame中?
我是python和pandas的新手。我正在尝试将tsv
文件加载到熊猫中DataFrame
。
这是我正在尝试的错误:
>>> 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!
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!
回答 0
注:由于17.0 from_csv
气馁:使用pd.read_csv
替代
该文档列出了一个.from_csv函数,该函数似乎可以执行您想要的操作:
DataFrame.from_csv('c:/~/trainSetRel3.txt', sep='\t')
如果您有标题,则可以传递header=0
。
DataFrame.from_csv('c:/~/trainSetRel3.txt', sep='\t', header=0)
Note: As of 17.0 from_csv
is discouraged: use pd.read_csv
instead
The documentation lists a .from_csv function that appears to do what you want:
DataFrame.from_csv('c:/~/trainSetRel3.txt', sep='\t')
If you have a header, you can pass header=0
.
DataFrame.from_csv('c:/~/trainSetRel3.txt', sep='\t', header=0)
回答 1
从17.0开始from_csv
不建议使用。
使用pd.read_csv(fpath, sep='\t')
或pd.read_table(fpath)
。
As of 17.0 from_csv
is discouraged.
Use pd.read_csv(fpath, sep='\t')
or pd.read_table(fpath)
.
回答 2
使用read_table(filepath)
。默认分隔符是制表符
Use read_table(filepath)
. The default separator is tab
回答 3
试试这个
df = pd.read_csv("rating-data.tsv",sep='\t')
df.head()
您实际上需要修复sep参数。
Try this
df = pd.read_csv("rating-data.tsv",sep='\t')
df.head()
You actually need to fix the sep parameter.
回答 4
打开文件,另存为.csv,然后应用
df = pd.read_csv('apps.csv', sep='\t')
对于任何其他格式,只需更改sep标记
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
回答 5
df = pd.read_csv('filename.csv', sep='\t', header=0)
您可以通过指定分隔符和标头将tsv文件直接加载到pandas数据框中。
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.