I have roughly 8000 gzipped files that are basically .txt files. Each files contains something in the order of 5 million rows. Additionally, each row either has 7 or 10 columns. The number of columns is defined by one column which has the value -1 or 1 respectively.
The thing is I need to load all files, do some filtering (since I don't need everything in each file), and then concat everything in the end to do my final analysis.
What I do now is something like this:
gz = gzip.open(f'{path}/{filename}')
df = pd.read_csv(gz, delimiter="\t")
This is by far the step of all the steps I am doing that takes the longest time. Reading a 5 million row gzipped file. This takes about 15s in this case for each file. However, I know in advance whether or not the things I need from files are either -1 or 1 in the particular column I talked about above. And to filter out all of those that are e.g. not -1 I can just do:
df = df[df["col1"] != 1]
But this happens AFTER I have loaded the entire file. And depending on whether it's -1 or 1 there is a reduction of either 25% or 75% in data, respectively.
Again, since the "bottleneck" are the read_csv part, I thought that by not loading either 25% or 75% of the data I could reduce the loading time. Because when everything is loaded df = df[df["col1"] != 1] only takes a ms or something.
So is there any way to filter out these particular rows while loading, or will that not give any noticable performance boost at all ? Or are there even smarter ways to load this in a faster way ?