【问题标题】:How to read a small percentage of lines of a very large CSV. Pandas - time series - Large dataset如何读取非常大的 CSV 的一小部分行。 Pandas - 时间序列 - 大型数据集
【发布时间】:2019-05-23 12:41:44
【问题描述】:

我在一个大文本文件中有一个时间序列。 该文件超过 4 GB。

因为它是一个时间序列,我只想阅读 1% 的行。

所需的极简主义示例:

df = pandas.read_csv('super_size_file.log',
                      load_line_percentage = 1)
print(df)

想要的输出:

>line_number, value
 0,           654564
 100,         54654654
 200,         54
 300,         46546
 ...

加载后我无法重新采样,因为一开始加载它需要太多内存。

我可能想逐块加载并重新采样每个块。但对我来说似乎效率低下。

欢迎任何想法。 ;)

【问题讨论】:

  • read_csv 有一个 nrows arg 和 chunksize,你有没有尝试过这些:pandas.pydata.org/pandas-docs/stable/reference/api/…
  • 你可以运行linux head命令,阅读head super_size_file.log > small_sample.loghead -n 1000 super_size_file.log > small_sample.log
  • @EdChum:nrows 加载 n 第一行。我想全部加载,但 100 行中只有 1 行... chunkzise 很棒,但加载每个块需要时间。 (99% 我不想要)。但这绝对是我的 B 计划。 @ sh.jeon:linux 中的'head' 似乎与nrows 相同。 (有趣的顺便说一句,但从我的角度来看同样的评论)

标签: python pandas time-series bigdata


【解决方案1】:

每当我必须处理一个非常大的文件时,我都会问“Dask 会做什么?”。

将大文件加载为dask.DataFrame,将索引转换为列(由于无法进行完全索引控制而导致的解决方法),然后过滤该新列。

import dask.dataframe as dd
import pandas as pd

nth_row = 100  # grab every nth row from the larger DataFrame
dask_df = dd.read_csv('super_size_file.log')  # assuming this file can be read by pd.read_csv
dask_df['df_index'] = dask_df.index
dask_df_smaller = dask_df[dask_df['df_index'] % nth_row == 0]

df_smaller = dask_df_smaller.compute()  # to execute the operations and return a pandas DataFrame

这将为您提供较大文件中的第 0、100、200 行等。如果您想将 DataFrame 减少到特定列,请在调用计算之前执行此操作,即dask_df_smaller = dask_df_smaller[['Signal_1', 'Signal_2']]。您还可以使用 scheduler='processes' 选项调用计算以使用 CPU 上的所有内核。

【讨论】:

    【解决方案2】:

    您可以在使用 read_csv pandas 函数时输入要读取的行数。这是你可以做的:

    import pandas as pd
    # Select file 
    infile = 'path/file'
    number_of_lines = x
    # Use nrows to choose number of rows
    data = pd.read_csv(infile,, nrows = number_of_lines*0.01)
    

    如果你想像你提到的那样逐块读取数据,你也可以使用 chunksize 选项:

    chunksize = 10 ** 6
    for chunk in pd.read_csv(filename, chunksize=chunksize):
        process(chunk)
    

    【讨论】:

      【解决方案3】:

      看看Iterating through files chunk by chunk。 它包含一个优雅的描述如何以块的形式读取 CSV 文件。

      基本思想是传递 chunksize 参数(每个块的行数)。 然后,在一个循环中,你可以逐块读取这个文件。

      【讨论】:

        【解决方案4】:

        这应该做你想做的。

        # Select All From CSV File Where
        
        import csv
        # Asks for search criteria from user
        search_parts = input("Enter search criteria:\n").split(",")
        # Opens csv data file
        file = csv.reader(open("C:\\your_path\\test.csv"))
        # Go over each row and print it if it contains user input.
        for row in file:
            if all([x in row for x in search_parts]):
                print(row)
        
        # If you only want to read rows 1,000,000 ... 1,999,999
        read_csv(..., skiprows=1000000, nrows=999999)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-19
          • 2017-10-08
          • 1970-01-01
          • 2016-11-23
          • 2018-03-31
          • 1970-01-01
          相关资源
          最近更新 更多