pandas 中的 read_table 和 read_csv 有区别吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41681250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:47:41  来源:igfitidea点击:

Is there a difference between read_table and read_csv in pandas?

python-3.xpandas

提问by gsa

I've tested it and also checked the documentation with no visible differences.Either way i wanted to ask just in case.

我已经对其进行了测试,并且还检查了文档,但没有明显差异。无论哪种方式,我都想问问以防万一。

Do you think that read_csv should be used only for csv's even though it works for other types? while read_table works for anything? and if they're the same while they exist?

你认为 read_csv 应该只用于 csv 吗,即使它适用于其他类型?而 read_table 对任何事情都有效?如果它们存在时相同?

回答by EdChum

You can get either to work for general delimited files, the difference are the default params, for instance sepis '\t'(tab) for read_tablebut ','for read_csv. They're both implemented the same underneath

您可以为一般分隔文件工作,不同之处在于默认参数,例如sep'\t'(tab) for read_tablebut ','for read_csv. 它们都在下面实现相同

If you look at the source

如果你看源码

they call the same function with different separators:

他们使用不同的分隔符调用相同的函数:

read_csv = _make_parser_function('read_csv', sep=',')
read_csv = Appender(_read_csv_doc)(read_csv)

read_table = _make_parser_function('read_table', sep='\t')
read_table = Appender(_read_table_doc)(read_table)

and _make_parser_function:

_make_parser_function

def _make_parser_function(name, sep=','):

is a general method which accepts the separg

是接受separg的通用方法

回答by Ay?e Nur

If you check out the Pandas documentation for read_table:

如果您查看Pandas 文档read_table

Deprecatedsince version 0.24.0.

Use pandas.read_csv()instead, passing sep='\t'if necessary.

自 0.24.0 版起已弃用

使用pandas.read_csv()替代,传递sep='\t',如果必要的。

So it is advised notto use read_table().

所以建议不要使用read_table()

回答by timgeb

The onlydifference is in fact the default value for the separgument.

唯一的区别是事实上的默认值sep参数。

read_csvuses sep=',', read_tableuses sep='\t'and that's it.

read_csv使用sep=','read_table使用sep='\t',就是这样。

We can confirm this with the help of the inspectmodule by getting the signature parametersas ordered mappings.

我们可以在inspect模块的帮助下通过获取签名参数作为有序映射来确认这一点。

import inspect                                                                                                     
import pandas as pd                                                                                                

params_csv = inspect.signature(pd.read_csv).parameters                                                                
params_table = inspect.signature(pd.read_table).parameters

There are only two elements in the symmetric difference of the parameters which both correspond to the separgument and its different default value for the two functions.

参数的对称差异中只有两个元素,它们都对应于sep两个函数的参数及其不同的默认值。

>>> params_csv.items() ^ params_table.items()                                                                                
{('sep', <Parameter "sep=','">), ('sep', <Parameter "sep='\t'">)}