【问题标题】:(Python/Pandas) Is line count similar to row count in pandas(Python/Pandas) 行数是否类似于 pandas 中的行数
【发布时间】:2017-09-06 22:01:53
【问题描述】:

有几种方法可以获取 pandas 数据帧的行数。

我的问题是;我可以通过简单地知道文件的行数来获得实际的行数吗?如果没有,有什么方法可以在不加载数据帧的情况下找到行数?

在这种情况下,我能够使行数和行数相同。

...
s1 = len(df.index) # get the row count.

with open(filename) as f: # open a file and count the lines, actual line count is 6377
    for i, l in enumerate(f):
        pass
s2 = i + 1 # row count
s2 = s2 - 2 # line count -2

输出:

s1 = {int} 6375
s2 = {int} 6375

【问题讨论】:

  • 要回答您的问题,您只需打开文件并查看其内容即可。例如,对于 csv,第一行代表标题,其他代表实际数据,所以对于这种情况 row count = line_count -1

标签: python pandas line-count


【解决方案1】:

不一定。例如,文件可能有标题,在这种情况下,它会比数据框多一行。根据从文件中读取数据框的方式,还有其他方法可以让 Pandas 忽略文件中的行(例如,您可以让它忽略注释行)。

【讨论】:

    猜你喜欢
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2023-01-27
    • 2015-12-17
    • 2019-12-19
    • 1970-01-01
    相关资源
    最近更新 更多