python - 如何将 tsv 文件加载到 Pandas DataFrame 中?

标签 python pandas csv

我是 python 和 pandas 的新手。我正在尝试将 tsv 文件加载到 pandas 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!

最佳答案

.read_csv功能做你想做的事:

pd.read_csv('c:/~/trainSetRel3.txt', sep='\t')

如果有 header ,可以传递header=0

pd.read_csv('c:/~/trainSetRel3.txt', sep='\t', header=0)

注意:在 17.0 之前,使用了 pd.DataFrame.from_csv(现已弃用,the .from_csv documentation link 重定向到 pd.read_csv)。

关于python - 如何将 tsv 文件加载到 Pandas DataFrame 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9652832/

相关文章:

python - 套接字火箭(iOS): How to identify whom user are chatting with another?

python - 'form'的值必须继承自 'BaseModelForm'

python - 具有不同时间索引的 Pandas 系列

python - 将 JSONL 文件转换为 CSV - "JSONDecodeError: Extra data"

Python:无法正确读取 CSV 文件

python - 如何使用 Python 删除 XPath 元素中的错误数据?

python - 当我们循环遍历所有元素时保留前 N 个元素

python - 类型错误 : bad operand type for unary ~: 'float'

python - 基于标签的索引 Pandas (.loc)

c++ - 从 C++ 中的 CSV 文件中读取特定元素