(Python/Pandas)行计数与Pandas中的行计数类似

2024-05-01 22:06:17 发布

您现在位置:Python中文网/ 问答频道 /正文

有几种方法可以获取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

输出:

^{pr2}$

Tags: 文件the数据方法pandasdfindexlen
1条回答
网友
1楼 · 发布于 2024-05-01 22:06:17

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

相关问题 更多 >