Python Pandas reads_csv skip first x and last y rows

2024/10/2 18:27:30

I think I may be missing something obvious here, but I am new to python and pandas. I am reading a large text file and only want to use rows in range(61,75496). I can skip the first 60 rows with

keywords = pd.read_csv('keywords.list', sep='\t', skiprows=60)

How can I only include the rows inbetween these values? There unfortunately is no userows parameter.

Is there something like

range(start, stop, start, stop)?
Answer

From the documentation, you can skip first few rows using

skiprows = X

where X is an integer. If there's a header, for example, a few rows into your file, you can also skip straight to the header using

header = X

Skip rows starting from the bottom of the file and counting upwards using

skipfooter = X

All together to set the header to row 3 (and skip the rows above) and ignore the bottom 4 rows: pd.read_csv('path/or/url/to/file.csv', skiprows=3, skipfooter=4)

https://en.xdnf.cn/q/70825.html

Related Q&A

Combine two arrays data using inner join

Ive two data sets in array: arr1 = [[2011-10-10, 1, 1],[2007-08-09, 5, 3],... ]arr2 = [[2011-10-10, 3, 4],[2007-09-05, 1, 1],... ]I want to combine them into one array like this: arr3 = [[2011-10-10, 1…

How to change fontsize of individual legend entries in pyplot?

What Im trying to do is control the fontsize of individual entries in a legend in pyplot. That is, I want the first entry to be one size, and the second entry to be another. This was my attempt at a so…

Split array into equal sized windows [duplicate]

This question already has answers here:Sliding window of M-by-N shape numpy.ndarray(8 answers)Closed 10 months ago.I am trying to split an numpy.array of length 40 into smaller, equal-sized numpy.array…

Is there a way to send a click event to a window in the background in python?

So Im trying to build a bot to automate some actions in a mobile game that Im running on my pc through Bluestacks.My program takes a screenshot of the window, looks for certain button templates in the …

how to store an image into redis using python / PIL

Im using python and the Image module(PIL) to process images.I want to store the raw bits stream of the image object to redis so that others can directly read the images from redis using nginx & htt…

Delete OCR word from Image (OpenCV,Python)

So, from what I can begin..I am working with OCR. The script works pretty well for what I need. It detects the words with an accuracy which for me is ok.This is the result: 100% accuracy with attached …

pandas.to_datetime inconsistent time string format

I am attempting to convert the index of a pandas.DataFrame from string format to a datetime index, using pandas.to_datetime().Import pandas:In [1]: import pandas as pdIn [2]: pd.__version__ Out[2]: 0.1…

Python NLTK WUP Similarity Score not unity for exact same word

Simple code like follows gives out similarity score of 0.75 for both cases. As you can see both the words are the exact same. To avoid any confusion I also compared a word with itself. The score refuse…

SystemExit: 2 error when calling parse_args() in iPython Notebook

Im learning to use Python and scikit-learn and executed the following block of codes (originally from http://scikit-learn.org/stable/auto_examples/document_classification_20newsgroups.html#example-docu…

How to convert numpy datetime64 [ns] to python datetime?

I need to convert dates from pandas frame values in the separate function:def myfunc(lat, lon, when):ts = (when - np.datetime64(1970-01-01T00:00:00Z,s)) / np.timedelta64(1, s)date = datetime.datetime.u…